LINQ嵌套在哪里

编程入门 行业动态 更新时间:2024-10-25 16:30:14
本文介绍了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嵌套在哪里

本文发布于:2023-11-16 21:56:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1607628.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   LINQ

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!