Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linq how to select a parent with a child collection that contains one or many of an array (or list) of values

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"));
like image 938
jason Avatar asked Oct 25 '11 13:10

jason


1 Answers

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")

like image 156
musefan Avatar answered Sep 19 '22 23:09

musefan