0

I would like to get all values which Intersect from this LINQ query:

Where(x => x.Attribute("name").Value).Intersect(myList).Any();

I don't know how to add a select.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Daniel Camacho
  • 423
  • 5
  • 12
  • 27

2 Answers2

2

I'm assuming the you want all the Attribute("name").Value that are in myList

You can either do

Where(x=>myList.Contains(x.Attribute("name").Value)).Select(x=>x.Attribute("name").Value)

or

Select(x=>x.Attribute("name").Value).Intersect(myList)
Bob Vale
  • 18,094
  • 1
  • 42
  • 49
  • The second answer gets and error: _Cannot convert lambda expression to delegate type 'System.Func' because some of the return types in the block are not implicitly convertible to the delegate return type._ Is there any solution because it seems a easier solution. (First works) – Daniel Camacho Jul 05 '12 at 10:53
  • @kmxillo I did a typo, missed the `x=>`. I've updated the answer – Bob Vale Jul 05 '12 at 10:57
1

Why do you do intersection? Why don't just List.Contains?

data.Where(x => myList.Contains(x.Attribute("name").Value).ToList();

or

data.Select(x => x.Attribute("name").Value).Intersect(myList);
abatishchev
  • 98,240
  • 88
  • 296
  • 433