score:7
The simplest way would be to do this:
foreach(var item in bList) aList.Remove(item);
Remove
doesn't do anything if the item isn't already in the list.
If by "use LINQ" you mean use LINQ to determine if an item in aList
is in bList
, that's not necessary.
A LINQ solution would be more efficient ONLY if both of these conditions are true:
aList
is very small compared tobList
- All of the items in
aList
that are also inbList
are at the very beginning ofbList
score:0
Check out the LINQ "Except" extension:
http://www.dotnetperls.com/except
Except subtracts elements from a collection. This extension method is found in the System.Linq namespace in the .NET Framework. It essentially subtracts all the elements in one collection from another.
score:8
Take a look at List<T>.RemoveAll
aList.RemoveAll(x => bList.Contains(x))
FWIW, this will also remove all objects in aList
contained in bList
, not only the first instance of each bList
object in aList
, if this is important.
score:2
You can use the Except
extension method:
var result = aList.Except(bList);
Note that you can also pass an IEqualityComparer<T>
as a second argument if you need to customize the equality checking.
Source: stackoverflow.com
Related Articles
- LINQ: delete from a list elements contained in another list
- Using Linq query inside List<T>.AddRange to conditionally add elements from one list to another
- LINQ in C#. Check if one list contains elements from another one
- Select list elements contained in another list in linq
- Linq to entities to return a list that contains elements from another list
- Cannot exclude from a list items contained in another list with Linq
- 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
- c# Linq or code to extract groups from a single list of source data
- How to construct a LINQ Query to test a list against another list where elements start with the elements from the other
- LINQ query to find if items in a list are contained in another list
- Create a list of one object type from a list of another using Linq
- C# LINQ select from where value is not contained in array / list
- Linq - How to select items from a list that contains only items of another list?
- Check if one list contains any elements from another
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- LINQ select List where sub-list contains item from another list
- Linq getting a list from another list
- Assign values from one list to another using LINQ
- Using LINQ to delete an element from a ObservableCollection Source
- Why does adding a list to another list, using add range, remove the elements from the first list?
- Get groups of 4 elements from name value list using LINQ in C#
- How to generate a unique list of items from another list using LINQ in C#
- How do I use Linq to find the elements of a list that are not present in another list?
- Select items from a List where the children contain the items from another List using LINQ
- How to select a distinct list of elements contained in another list?
- How to fill a property of an object from another list using LINQ
- How to use LINQ to query list of strings that do not contain substring entries from another list
- Create a List of elements from a DataTable LINQ Column
- Get the count of distinct elements from a list of lists using LINQ
- Quick way to check 2 lists are the same c#
- How do I access consecutive elements in an IQueryable<T> object?
- Linq query syntax equivalent group by id having sum(amount) > x amount
- LinqDataSource: How to assign values to where parameters in code?
- Initialize a Linq to Sql object via business logic
- Left Join, Group By and Sum where related records don't exist
- How can I get distinct objects extracted from a property of a list of objects?
- Conditional Join In LINQ?
- Create a list of one object type from a list of another using Linq
- Using Func property selector with Entity Framework 6
- Using LINQ to copy data from one list to another
- Value can not be null exception
- EF join works but SelectMany doesn't
- How to filter related data using Entity Framework and LINQ to SQL and LinqKit PredicateBuilder Or IdeaBlade DevForce
- How to read .csv file into array using LINQ
- How can I convert anonymous type to strong type in LINQ?
- foreach statement cannot operate on variables of type 'object'
- How do I get an Object from the Primary Key
- Linq Nullable object must have a value. errors in .NET 6 and EF Core
- How can I group a List<K> by a List<T>, depending on the results of an expression<T, K>?