LINQ如何选择与包含一个或多个阵列的(或列表)的子集的值的父多个、子集、阵列、如何选择

2023-09-04 02:37:14 作者:人间皆苦

这似乎将是很容易

  VAR ORX = gg.Where(X => x.ProductAttributes.Any(PA => pa.AttributeId ==家));
 

返回GG当产品属性有家

我需要它从一个数组返回,其中和GG拥有产品属性值 即

  VAR ORX = gg.Where(X => x.ProductAttributes.Any(PA => pa.AttributeId在家庭,工作));
 
excel中如何制作列表对应关联

解决方案

怎么样......

 的String []值=新的String [] {家庭,工作};
VAR ORX = gg.Where(X => x.ProductAttributes.Any(PA => values​​.Contains(pa.AttributeId));
 

甚至家庭,工作。载(pa.AttributeId)应该工作,如果你的列表是可靠的,你的榜样。 (我决不意味着推荐这个除非你能保证AttributeId不会被任何清单的话的一个子......比如我)

This seems like it would be easy enough

var orx = gg.Where(x=>x.ProductAttributes.Any (pa =>pa.AttributeId == "home"));

returns gg when product attributes has a value of "home"

I need it to return where and gg has product attribute values from an array i.e.

var orx = gg.Where(x=>x.ProductAttributes.Any (pa =>pa.AttributeId in "home,work"));

解决方案

what about...

string[] values = new string[] { "home", "work" };
var orx = gg.Where(x => x.ProductAttributes.Any(pa => values.Contains(pa.AttributeId));

or even "home,work".Contains(pa.AttributeId) should work, if your list is as reliable as your example. (I by no mean recommend this unless you can ensure that AttributeId will not be a substring of any of the list words.. such as "me")