.NET C# 我想从一个数据中获取 ID(1/多个),如果它与另一个 table

.NET C# I want to get the IDs (1/multiple) from a data if it much the ID of another table

型号:

namespace practise_API.Model
{
    public class SourceAttributes
    {
        [Key]
        public int SourceEntityId { get; set; }

        public int ATiD { get; set; }
        public string Name { get; set; }
        public string Datatype { get; set; }
    }

    public class OtherData
    {
        public int ID { get; set; }
        public string Ename { get; set; }
        public string Type { get; set; }
        public string Source { get; set; }
        public string Frequency { get; set; }
    }
}

IRepository接口:

namespace API.Repository
{
    public interface ISourceRepository
    {
        IEnumerable<SourceEntities> GetSourceEntities();
        IEnumerable<SourceAttributes> GetSourceAttributes(int id);

        ........
        void Save();
    }
}

Repository 实施:

public class SourceRepository : ISourceRepository
{
    private readonly MapperDbContext _dbContext;

    public SourceRepository(MapperDbContext dbContext)
    {
        _dbContext = dbContext;
    }

    public IEnumerable<SourceAttributes> GetSourceAttributes()
    {
        return _dbContext.SourceAttributes
                         .Where(i => i.SourceEntityId == OtherData.ID)
                         .ToList();
    }
}

我想从 OtherData 中获取与该 ID 匹配的所有 SourceAttributes.SourceentityIds。我想到的方法是使用 where() System.Linq 但我无法获得 OtherData.ID.

此外,这将稍后在我的 Get() 控制器中调用。

ISourceRepository.GetSourceAttributes 有一个 id 参数,但实现缺少它肯定会导致编译器错误。 修改它:

public IEnumerable<SourceAttributes> GetSourceAttributes(int otherDataId)
{
   return _dbContext.SourceAttributes
      .Where(i => i.SourceEntityId == otherDataId)
      .ToList();
}

获取你想要的ID:

var ids = sourceRepository
            .GetSourceAttributes(myOtherDataInstance.ID)
            .Select(i => i.ID)
            .ToArray();   // or leave it as IEnumerable as you wish