简体   繁体   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

Models:楷模:

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 interface: IRepository接口:

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

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

Repository implementation: 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();
    }
}

I want to get all the SourceAttributes.SourceentityIds that match that one ID from OtherData .我想从OtherData SourceAttributes.SourceentityIds The way I thought about it was to use where() System.Linq but I can't get the OtherData.ID.我想到的方法是使用 where() System.Linq 但我无法获得 OtherData.ID。

Also this is going to be called later in my Get() of my controller.这也将在我的 controller 的Get()中调用。

The ISourceRepository.GetSourceAttributes has an id parameter, but the implementation is missing it resulting surely in a compiler error. ISourceRepository.GetSourceAttributes有一个id参数,但实现缺少它肯定会导致编译器错误。 Modify it:修改它:

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

To get the ids you want:要获取您想要的 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.

相关问题 我想从oracle数据库中获取多个数据,并将其存储到C#.net中的另一个变量中 - I want to fetch multiple data from oracle database and want to store it into another variables in C# .net 我想得到一个字段值让我们说(名称)来自C#Mvc中的数据表, - I want to get A field Values let say (Name) From Data Table in C# Mvc, 我想使用asp.net mvc 5将一个表中的ID保存到另一个表中 - I want to save an ID from a table to another table using asp.net mvc 5 我想从表中获取行 ID - I want to get row Id from Table 我想在asp.net和C#中使用WCF一次添加多个图像和其他数据 - I want to Add multiple images and other data at a time using WCF in asp.net and C# 我想在C#中获取对象/变量的创建者线程id - I want to get creator thread id of object/variable in C# 如何使用 VSTS Git API for C# 获取给定提交 ID 的所有拉取请求 ID(来自合并提交) - How can I get all pull requests Ids (from merged commits) for a given commit Id using VSTS Git API for C# 想要使用 C# 和 Windows Forms 将两个不同表中的 ID 添加到一个关联表中 - Want to add IDs from two different tables into one associative table using C# and Windows Forms C#:如何从表中获取嵌套数据? - C#: How do I get a nested data from table? 在ASP.NET C中的另一个查询中创建#temp表之后,从#Temp表中选择数据 - select data from #Temp table after #temp table create in another query in asp.net c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM