两个简单的查询 - 例外情况发生在:
matchings.Any(u => product.ProductId == u.ProductId)
Run Code Online (Sandbox Code Playgroud)
怎么了?如果我写作true而不是一切都很好.
var matchings = (from match in db.matchings
where match.StoreId == StoreId
select match).ToList();
var names = (from product in db.Products
where matchings.Any(u => product.ProductId == u.ProductId)
select product).ToList();
Run Code Online (Sandbox Code Playgroud)
Rap*_*aus 50
第一种方式:
ToList()在第一个查询中删除.
要么
//instead of retrieving mathings List, retrieve only the productIds you need (which are a List of Primitive types)
var productIdList = db.matchings
.Where(m => m.StoreId == StoreId)
.Select(x => x.ProductId)
.ToList();
var products = db.Products
.Where(p => productIdList
.Contains(p.ProductId))
.ToList();
Run Code Online (Sandbox Code Playgroud)
要么
//other way
var produts = db.Products
.Where(p => db.matchings
.Any(m => m.StoreId == StoreId &&
m.ProductId == p.ProductId)
)
.ToList();
Run Code Online (Sandbox Code Playgroud)
因为我认为你在linq2entities,并且你在查询中使用匹配列表是不可能的(你的主题的标题往往让我相信这是你的问题).
这看起来像是一个使用join的地方
var query =
from product in db.Products
join matching in db.Matchings
on product.ProductId equals matching.ProductId into matchGroup
where matchGroup.Count() > 0 and matching.StoreId == StoreId
select product;
Run Code Online (Sandbox Code Playgroud)