LINQ查询匹配内部集合中的值

时间:2017-10-16 20:18:35

标签: linq

寻找LINQ实现来处理以下用例。

Pesudo对象的定义:

class Product{
int Id;
string Name;
IList<ProductAttribute> ProductAttributes;
}

class ProductAttribute{
int Id;
string Name;
string Value;
}

我收集的产品类似于:

[
{"Name":"Red Large Shirt", "ProductAttributes": [{"Name": "Color", Value:"Red"},{"Name":"Size", "Value":"Large"}, {"Name":"Type", "Value":"Shirt"}]},
{"Name":"Red Small Shirt", "ProductAttributes": [{"Name": "Color", Value:"Red"},{"Name":"Size", "Value":"Small"}, {"Name":"Type", "Value":"Shirt"}]},
{"Name":"Red Large Pant", "ProductAttributes": [{"Name": "Color", Value:"Red"},{"Name":"Size", "Value":"Large"}, {"Name":"Type", "Value":"Pant"}]},
{"Name":"Blue Large Shirt", "ProductAttributes": [{"Name": "Color", Value:"Blue"},{"Name":"Size", "Value":"Large"}, {"Name:"Type", "Value":"Shirt"}]},
]

我需要一个LINQ语句来返回与某些属性匹配的产品列表。 例如。返回ProductAttribute产品列表(&#34;名称&#34; =&#34;尺寸&#34;&amp;&amp;&#34;价值&#34; =&#34;大&#34;)&amp;&amp; (&#34;名称&#34; =&#34;颜色&#34;&amp;&amp;&#34;价值&#34; =&#34;红色&#34;)

由于

1 个答案:

答案 0 :(得分:1)

您需要使用.Any().Contains()扩展名。例如,

products.Where(p=>p.ProductAttributes.Any(a=>a...condition here for attributes))