简体   繁体   English

根据另一个查询结果的结果过滤linq查询

[英]filter a linq query based on the results of another query's results

I am wanting to filter a linq query 我想过滤一个linq查询

I have 2 linq statements 我有2个linq语句

The 1st gets all the stores I want and the 2nd is where I filter information based on the results found in the 1st query. 第一个获取我想要的所有商店,第二个是我根据第一个查询中找到的结果过滤信息的地方。

var stores = ctx.Stores.Where(ps => ps.ParentStoreID == parent.ParentStoreID && ps.StoreID!=storeID);

var query = (from a in ctx.TransactionTable
          from b in ctx.MappingTable.Where(x => x.TransactionId== a.TransactionId).DefaultIfEmpty()
             where a.StoreID!=storeID
                 select new
                           {
                              Transactions = a,
                              Mapping = b
                           }).ToList();

How do I add another where clause into my 2nd query to only return results where a.StoreId is contained within the stores result? 如何在第二个查询中添加另一个where子句,仅返回存储结果中包含a.StoreId的结果?

Like this: 像这样:

var stores = ctx.Stores.Where(ps => ps.ParentStoreID == parent.ParentStoreID && ps.StoreID!=storeID);

var query = (from a in ctx.TransactionTable
            from b in ctx.MappingTable.Where(x => x.TransactionId==a.TransactionId).DefaultIfEmpty()
            where a.StoreID!=storeID && stores.Select(s => s.StoreID).Contains(a.StoreID)
            select new
            {
                Transactions = a,
                Mapping = b
            }).ToList();

You can find more info here: Linq to Entities - SQL "IN" clause 您可以在此处找到更多信息: Linq to Entities - SQL“IN”子句

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

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