LINQ嵌套在哪里 [英] LINQ Nested Where

查看:63
本文介绍了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嵌套在哪里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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