score: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>
{ ... }
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 oflist2
, and returnstrue
iftrue
has been returned for any item from the list- The predicate inside
Any
tests foritem
's name to be equal to that ofcmd
.
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
}
Source: stackoverflow.com
Related Articles
- Using LINQ to filter a list of items based on those items' presence in another list
- Return items in one list based upon their matching index in another using Linq
- Filter a list based on another list containing IEnumerable<Guid> using linq
- How to filter a list based on another list using Linq?
- Group items in a list based on two properties using LINQ
- How to generate a unique list of items from another list using LINQ in C#
- checking if all the items in list occur in another list using linq
- Select items from a List where the children contain the items from another List using LINQ
- Using LINQ to get a list of items where the item contains a part of an item from another list
- LINQ Filter results based on values between another list
- Filter Out List with another list using LINQ
- Using Linq to object, how can I get one field's value based on another in the same list
- Using Lambda / Linq Filter Parent Collection based on Child Items
- Filter a list of objects using a property that is another list. Using linq
- How to filter linq query based on all of the values of one property from another list
- Using LINQ to remove items from a list that do not apear in another list
- Linq filter a list of objects based on another list
- Filter one list using index value obtained from another list using linq
- Filter LINQ query using items from an external list with Lambda
- How do you filter a list based on matching items in another list?
- How to filter a list by comparing a value from another list using Linq in C#?
- Using Linq to Filter a List of Objects based on a Condition
- How to use LINQ to filter a list of items in another list and not in a third
- Filter List using linq for remove duplicates items
- How do optimize nested loop and filter from another list using linq c#
- Get list items by checking priority of another list using linq
- How do i filter one list from another list using linq
- c# How to get count of value in a List based on value in another List using LINQ
- How to set order of IQueryable query based on another list using linq in c#?
- In Linq search a list using items in another list
- Linq OrderBy(Byte[]) values
- LINQ .Startswith or .Contains problems in VB.NET4
- LINQ to Entities - How to return a single string value from entity
- Why I get another reference each time I call IEnumerable.Last()
- linq conditional query
- Linq and EF on .Any()
- Foreach updates every record in a LINQ result
- Linq Conditional .Any() Select
- How to modify only one or two field(s) in LINQ projections?
- Comparing two lists using linq based on Value
- LINQ get items in List<AttributeValuePair>
- net core Linq dynamic Sort
- How to do a loop to sort and update list<> based on specific condition
- Error Message: "Only primitive types or enumeration types are supported in this context."
- LINQ equivalent of List<T>.Find()?
- Boolean value for linq query sets to false but is true in SQL
- query for tag ids in model
- Proper way to structure a Sync Framework DAL
- join multiple tables using LINQ
- Is Aggregate fatally flawed because each into clause is executed separately?