14

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"));
sll
  • 61,540
  • 22
  • 104
  • 156
jason
  • 767
  • 2
  • 9
  • 24
  • possible duplicate of [Linq to Entities - Sql "IN" clause](http://stackoverflow.com/questions/857973/linq-to-entities-sql-in-clause) – jrummell Oct 25 '11 at 13:41

2 Answers2

25

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

musefan
  • 47,875
  • 21
  • 135
  • 185
  • `"home,work".Contains(pa.AttributeId)` will return wrong result if any of AttributeIds will be a part of `home` or `work` words, for instance `ork`... – sll Oct 25 '11 at 13:48
  • @sll: Yes, which is why I said "if your list is as reliable as your example" – musefan Oct 25 '11 at 13:53
  • 1
    That was the syntax I was missing, had it backwards – jason Oct 25 '11 at 14:52
5

Using Enumerable.Contains():

var orx = gg.Where(x => x.ProductAttributes
                        .Any(pa =>
                             array.Containspa(pa.AttributeId));

var orx = gg.Where(x => x.ProductAttributes
                        .Any(pa =>
                             "home, work".Split(',').Contains(pa.AttributeId));
sll
  • 61,540
  • 22
  • 104
  • 156