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 Query
- LINQ in C#. Check if one list contains elements from another one
- Check if one list contains any elements from another
- How to check if one list contains all elements of another list in linq to sql statement
- 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
- Check which elements are on one list comparing to another list LINQ
- Check if elements from one list elements present in another list
- 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
- Check if one IEnumerable contains all elements of another IEnumerable
- Create a list of one object type from a list of another using Linq
- Linq - How to select items from a list that contains only items of another list?
- Check if a list contains all of another lists items when comparing on one property
- LINQ select List where sub-list contains item from another list
- Assign values from one list to another using LINQ
- Check if a value from one array exists in another array using linq
- How to Remove elements from one List based another list's element and condition?
- Using LINQ to get a list of items where the item contains a part of an item from another list
- Linq return all records from entity where a field contains one or more words from a list
- check a string array from a list contains a string from another list or vice versa
- LINQ get elements from one collection that don't belong to another
- Using LINQ to copy data from one list to another
- Linq - Remove one anonymous list from another
- Elegant way to check if a list contains an object where one property is the same, and replace only if the date of another property is later
- To determine if one of the Strings from a list contains the initial part of a specified string using LINQ
- Getting one list from another list using LINQ
- LINQ Lamba Select all from table where field contains all elements in list
- Use linq to remove elements in one list using a condition in another
- How to remove elements of a list where its string contains sub strings from another list
- LINQ to remove elements from one collection that don't match elements in another collection based on a field
More Query from same tag
- How to call custom method in linq query
- Many-to-Many Select second order
- Foreach Loop to linq
- What is LINQ Actually Compiled To?
- c# use string parameter to define what property to filter by in List of objects
- ASP.NET MVC - Join Tables using LINQ
- Linq Join 2 tables, the column names are different but the values in the column are the same in both tables
- Ranking Search Results With LINQ/.NET MVC
- LINQ - Find index of a list item that has a null value
- Selecting only part of the DbSet in Read method of Kendo grid
- Adding a character at the beginning of a string if it does not exist using LINQ
- Unable to add item to dataset in Linq to SQL
- Updating a complex object with linq and Entity Framework
- Create Linq Obect Based On Another Linq Object
- Convert JSON object array value to string array in VB.NET without loop
- How can I create a LINQ-friendly 'return false' expression using C# expression trees?
- How to create dynamic Multiple And linq conditions in foreach loop
- Getting text between partial limiters/phrases
- Linq OrderBy doesn't return expected results
- Puzzling Problem - C# Lambda Delegates disappearing after
- Get the Matched Property Name in linq where with multiple condition
- What if IEnumerable<string> returns just one string
- I need help grouping my data using Linq
- Query- LINQ, 2 tables, Union
- Convert foreach to linq expression with select
- XDocument is removing document comment
- Error Trying to pass a IEnumerable list with Linq and Mvc Web API
- Add second group by key field to existing Lambda Expression
- EntityFunction.DiffDays Causes This function can only be invoked from LINQ to Entities
- Will this LINQ query enumerate through the entire enumeration?