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:3
I think you are looking for something like this
if(list2.Any(list2cmd=> list2cmd.Name == cmd.name))
{
// do stuff
}
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
}
score:5
if (list2.Any(x => x.Name == cmd.Name)) { }
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))) {
...
}
Source: stackoverflow.com
Related Query
- 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
More Query from same tag
- Add index position Value of an array of array using Linq
- Azure CosmosDB Contains method does not working
- How to chop up a string with LINQ
- Linq to SQL to return multiple counts from query not returning correct results
- How to expand calling expressions other than predicates?
- Is it necessary to use joins in Linq to Sql
- How to use LINQ to query matching items to a list?
- LINQ 2 SQL - using a C# entity collection and SqlMethods.Like together
- LINQ to Entities DateTime is inconsistent
- How can I order a List according to my own rules?
- Sql Not In in Linq Issue
- How to C# Linq Order by using list of strings similar to MySql FIELD
- Conversion to LINQ query
- Use of LINQ and ArrayList
- C#--Counting the number of occurrences of a single digit(int) in a string--not working with int to char converted Variable
- LINQ how to return group by as List?
- How Convert this T-SQL to LINQ
- When is a long line just too long?
- Dynamic lambda expression - compare properties in a List<T> postion 0 with properties in position 1
- Linq Lookup to parse a CSV text line
- Get list of months from List<DateTime>
- How do I most elegantly express left join with aggregate SQL as LINQ query
- optional include with entity framework
- Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>'
- Assiging an xml query result to a variable
- Add a Linq clause to a Iqueryable object
- using union on linq compiled queries
- Dynamic where clause to filter collection elements
- Xml Linq Query (get Element by Attribute and Attribute value)
- LINQ query: Dynamically add Includes at run time