繁体   English   中英

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

[英].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 SourceAttributes.SourceentityIds 我想到的方法是使用 where() System.Linq 但我无法获得 OtherData.ID。

这也将在我的 controller 的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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM