本文介绍了LINQ嵌套在哪里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有以下型号;
public List<RecommendedProduct> recommendations然后
public class RecommendedProduct public List<Product> Products然后是产品;
public class Product public string Code建议列表中有10个项目.
The recommendations list has, as an example, 10 items in it.
每个推荐项目中都有两个产品.
Each recommendations item has two Products in it.
如何使用LINQ查找包含产品代码同时为"A"和"B"的产品的建议对象?
How, with LINQ, can I find the recommendations object that has products with both "A" and "B" product codes?
推荐答案使用Any扩展名:
var myProducts = from rp in recommendations where cp.Products.Any(p => p.Product.Code == "A") && cp.Products.Any(p => p.Product.Code == "B") select rp;如果序列中有任何匹配内部条件的元素,则
Any返回true.在这种情况下,您要搜索两个元素,因此需要两个Any调用.
Any returns true if there are any elements in the sequence that match the inner condition. In this case you're searching for two elements, so it takes two Any calls.
更多推荐
LINQ嵌套在哪里
发布评论