score:5

Accepted answer

I think this is what you want.

if (list2.Any(l2c => l2c.Name == cmd.Name))
{ ... }

but you can add it to the foreach and avoid the if in your code:

foreach(Command cmd in list1.Where(l1c => list2.Any(l2c => l2c.Name == l1c.Name)))
{
    ... some code ...
}

If you control the Command class and can define equality in it (overriding Equals, etc), you can simply use Intersect:

foreach(var cmd in list1.Intersect(list2))
{ ... }

If you don't control Command or don't want to define equality in the class, you can still use Intersect with an IEqualityComparer

foreach(var cmd in list1.Intersect(list2, new CommandComparer()))
{ ... }

class CommandComparer : IEqualityComparer<Command>
{ ... }

score:5

if (list2.Any(x => x.Name == cmd.Name)) { }

score:3

I think you are looking for something like this

if(list2.Any(list2cmd=> list2cmd.Name == cmd.name))
{
    // do stuff
}

score:6

Try this:

if (list2.Any(item => item.Name == cmd.Name)) {
}

Here is the "anatomy" of the statement:

  • Any applies its condition to each item of list2, and returns true if true has been returned for any item from the list
  • The predicate inside Any tests for item's name to be equal to that of cmd.

You can fold the if into LINQ as well:

foreach (var cmd in list1.Where(c => list2.Any(item => item.Name == c.Name))) {
    ...
}

score:3

You can get the enumerable itself as:

var query = list1.Where(cmd => (list2.Any(item => item.Name == cmd.Name)));

Then just loop over query, which will contain every Command you want:

foreach( Command cmd in query)
{
    // Do something
}

Related Articles