score:11
Accepted answer
Use Enumerable.Intersect
:
var newList = OldIDs.Intersect(NewIDs).ToList();
If OldIDs
contains a custom object with an Id
property:
var newList = OldIDs.Where(x => NewIDs.Contains(x.Id)).ToList();
another, possibly more efficient way using Join
:
var oldInNew = from old in OldIDs
join newID in NewIDs
on old.Id equals newID
select old;
var newList = oldInNew.ToList();
score:2
You want to use Intersect()
, try the following:
var intersectedList = NewIDs.Intersect(OldIDs).ToList();
Source: stackoverflow.com
Related Articles
- LINQ in C#. Check if one list contains elements from another one
- Check if one list contains any elements from another
- Linq to entities to return a list that contains elements from another list
- LINQ check if a list contains any item from another list mysql syntax error
- How to check if one list contains all elements of another list in linq to sql statement
- Linq - How to select items from a list that contains only items of another list?
- LINQ select List where sub-list contains item from another list
- Using Linq query inside List<T>.AddRange to conditionally add elements from one list to another
- Check if one list contains all items from another list in order
- Using LINQ to get a list of items where the item contains a part of an item from another list
- Check which elements are on one list comparing to another list LINQ
- check a string array from a list contains a string from another list or vice versa
- Check if elements from one list elements present in another list
- LINQ Lamba Select all from table where field contains all elements in list
- How to remove elements of a list where its string contains sub strings from another list
- check List <int> A contains elements from list B, with repeated values
- How to use Linq to check a list of objects property have allowed values from another list
- C# / Unity / Linq - How to check if a list of lists contains a specific another list?
- Check if a string list contains any substrings from another string list
- Construct a list of wpf Hyperlink elements from an XML source file using Linq
- Turning a chain of foreach's for inserting elements from a list into another list into linq
- How to creat a new list from existing list with elements which contains the same id in Linq to object?
- Linq to check if all value from another list exist
- To remove records from a list who's column contains elements of the second list using Linq
- Check if text contains any string item from list of string c# linq
- c# Linq or code to extract groups from a single list of source data
- Dynamic linq query that Contains ALL from another list
- use linq to get a sub list that contains elements from an array
- How to construct a LINQ Query to test a list against another list where elements start with the elements from the other
- Check if one IEnumerable contains all elements of another IEnumerable
- How to merge two classes into single unit?
- Linq query: Group by Multiple Columns
- Retrieving an item from a list based on selected value
- How to pivot a list in C#
- VB.NET Cannot Convert ISingleResult to String
- Select new with an if statement inside
- Filter a list based on another list containing IEnumerable<Guid> using linq
- LINQ to DataSet, error in getting char field
- Where clause in LINQ query doesn't evaluate against data
- How to make a nested List form a List
- How to prevent Entity Framework from loading all child objects
- Show Data to dropdownlist - "LINQ to Entities does not recognize the method 'System.String ToString()' method"
- How to get average date difference in PostgreSQL using EF Core 3
- LINQ join based on checkbox
- How to select Fruit object from Fruit ID (unique)
- LINQ - Select anonymous type which based on a collection property
- Converting a given SQL Query to LINQ
- Pass a parameter to the reusable Expression in a Entity Framework
- Linq search result by closest match
- Simple search of XDocument