3

I'm trying to learn LINQ by practice. This seems like a situation where I should be able to use it, but I can't quite figure out if it's possible or if I'm barking up the wrong tree.

Can I achieve what's in the brackets [] with a one-liner LINQ query given the use case below?

List<Command> list1, list2;

PopulateCommandLists(list1, list2);

foreach(Command cmd in list1)
{
    if ([cmd.Name is present as the Name in any of list2's Command objects])
    {
        //some code.
    }
}
4

5 に答える 5

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))) {
    ...
}
于 2012-06-28T16:07:23.130 に答える
5
if (list2.Any(x => x.Name == cmd.Name)) { }
于 2012-06-28T16:07:01.463 に答える
5

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>
{ ... }
于 2012-06-28T16:07:01.743 に答える
3

列挙可能なもの自体は次のように取得できます。

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

次に、クエリをループします。これには、必要なものがすべて含まれCommandます。

foreach( Command cmd in query)
{
    // Do something
}
于 2012-06-28T16:12:01.603 に答える
3

I think you are looking for something like this

if(list2.Any(list2cmd=> list2cmd.Name == cmd.name))
{
    // do stuff
}
于 2012-06-28T16:07:03.197 に答える