简体   繁体   English

EF6 IN子句的特定属性

[英]EF6 IN clause on specific properties

Good morning, 早上好,

I'm having trouble with a EF query. EF查询出现问题。 This is what i am trying to do. 这就是我想要做的。

First i am pulling a list of ID's like so (List of IDs are found in the included x.MappingAccts entity): 首先,我像这样拉出一个ID列表(在包含的x.MappingAccts实体中找到ID列表):

Entities.DB1.Mapping mapping = null;
using (var db = new Entities.DB1.DB1Conn())
{
    mapping = db.Mappings.Where(x => x.Code == code).Include(x => x.MappingAccts).FirstOrDefault();
}

Later, i'm trying to do a query on a different DB against the list of Id's i pulled above (essentially a IN clause): 稍后,我试图对另一个数据库(上面是我拉的Id的列表)执行查询(本质上是IN子句):

using (var db = new Entities.DB2.DB2Conn())
{
    var accounts = db.Accounts.Where(mapping.MappingAccts.Any(y => y.Id == ?????????)).ToList();
}

As you can see i only got part way with this. 如您所见,我对此只有部分了解。

Basically what i need to do is query the Accounts table against it's ID column and pull all records that match mapping.MappingAccts.Id column. 基本上,我需要针对其ID列查询Accounts表,并提取所有与mapping.MappingAccts.Id列匹配的记录。

Most of the examples i am finding explain nicely how to do this against a single dimension array but i'm looking to compare specific columns. 我发现的大多数示例都很好地解释了如何针对单个维数组执行此操作,但是我正在寻找比较特定列的方法。

Any assist would be awesome. 任何协助都会很棒。

Nugs 猪兔

An IN clause is generated using a IEnumberable.Contains 使用IEnumberable生成IN子句。

From the first DB1 context, materialize the list of Id's 在第一个DB1上下文中,实现Id的列表

var idList = mapping.MappingAccts.Select(m => m.Id).ToList();

Then in the second context query against the materialized list of id's 然后在第二个上下文中查询ID的物化列表

var accounts = db.Accounts.Where(a => idList.Contains(a.Id)).ToList();

The only problem you may have is with the amount of id's you are getting in the first list. 您可能唯一的问题是您在第一个列表中获得的ID数量。 You may hit a limit with the sql query. 您可能会使用sql查询达到限制。

This will give the list of Accounts which have the Ids contained by MappingAccts 这将给出具有MappingAccts包含的ID的帐户列表

using (var db = new Entities.DB2.DB2Conn())
{
    var accounts = db.Accounts.Where(s => mapping.MappingAccts.Any(y => y.Id == s.Id)).ToList();
}

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

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