根据另一个查询的结果过滤linq查询 [英] filter a linq query based on the results of another query's results

查看:93
本文介绍了根据另一个查询的结果过滤linq查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要过滤linq查询

我有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();

如何在第二个查询中添加另一个where子句,以仅返回存储结果中包含a.StoreId的结果?

How do I add another where clause into my 2nd query to only return results where a.StoreId is contained within the stores result?

推荐答案

像这样:

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();

您可以在此处找到更多信息: Linq到实体-SQL"IN";条款

You can find more info here: Linq to Entities - SQL "IN" clause

这篇关于根据另一个查询的结果过滤linq查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆