且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

LINQ嵌套在哪里

更新时间:2022-03-17 09:46:27

使用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.