You don't need a join for that:
var filteredEmployees = groupA.Except(groupB);
Note that this will be a sequence of unique employees - so if there are any duplicates in
groupA, they will only appear once in
filteredEmployees. Of course, it also assumes you've got a reasonable equality comparer1. If you need to go specifically on name, you can use
ExceptBy from MoreLINQ:
var filteredEmployees = groupA.ExceptBy(groupB, employee => employee.Name);
Or without going into a third party library:
var groupBNames = new HashSet<string>(groupB.Select(x => x.Name)); var filteredEmployees = groupA.Where(x => !groupBNames.Contains(x.Name));
1 As pointed out in the comments, you can pass in an
IEqualityComparer<T> as an argument to
Except. I have a
ProjectionEqualityComparer class in MiscUtil which makes it easy to build a comparer of the kind you need:
// I can't remember the exact method name, but it's like this :) var comparer = ProjectionEqualityComparer<Employee>.Create(x => x.Name); var filteredEmployees = groupA.Except(groupB, comparer);
No, a "not equal" operator would get you all combinations of groupA and groupB except the ones where the items were the same.
Using the Except method gets you what you want:
var filteredEmployees = groupA.Except(groupB);
In Entity Framework 6, I've gotten better results using
var filteredEmployees = groupA.Where(a => !groupB.Select(b => b.Name).Contains(a.Name));
- using equal and not equal in a linq join
- LINQ Left Join On Not Equal Rows
- C# Linq Join not equal
- Self JOIN linq with not equal
- How to set Object instance value equal null if there is no data found in Left Join in LINQ Query
- linq query not returning anything by itself but data shows when there is a join added. What am I doing wrong?
- Why this multi linq join code does not work?
- New transaction is not allowed because there are other threads running in the session LINQ To Entity
- Is there a "not equal" in a linq join
- Is there a better way of calling LINQ Any + NOT All?
- LINQ left outer join query error: OuterApply did not have the appropriate keys
- How do you left join in Linq if there is more than one field in the join?
- How to write not equal operator in linq to sql?
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- Join tables in SQLite.Net with LINQ on xamarin.android is not supported
- Is there a good source that gives an overview of linq optimizations?
- SQL Query to LINQ syntax using not exist and join
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- Left outer join using LINQ -- understanding the code
- "the method join is not supported" with Tridion OData service & Linq
- LINQ Source Code Available
- LINQ Join to find items NOT IN a list
- LINQ LEFT JOIN where clause not working
- LINQ LEFT JOIN not working on NULL values
- Refactor Linq code and "LINQ to Entities does not recognize the method"
- DataTable select with LiNQ and check is there duplicate rows or not
- Linq Expressions: The binary operator Equal is not defined for the types 'MyEnum' and 'System.Enum'
- linq - how do you do a query for items in one query source that are not in another one?
- LINQ Join not returning results if second or third table empty
- Is there a way to use LINQ to select a range of items based on an enclosing condition (i.e. not a simple WHERE clause)?
More Query from same tag
- Apply PredicateBuilder to query a single object property of a list of items
- If condition in LINQ count clause using expression method
- Linq Select list of dynamic properties into another Object?
- Return a ReadOnlyDictionary sorted by value
- C# Linq Registry values to DataTable
- update object created in another datacontext
- Get Range of integers with linq
- C# LINQ statement with joins, group by and having then mapped into list object
- group by dates and make mathematics on it using linq
- Linq Sum based on some columns
- Best way to build a linq query based on user selection in VB
- Select Nodes with same names and read the Value of one Child of it
- How to query Anonymous Type collection?
- Linq query to sum by group
- C# rearrange List by pattern
- No overload for method 'select' takes 2 arguments
- Binding RadGridView with stored procedure's result in EntityFramework
- Can't get outer join to work in EF
- linq how to get values from var
- Sort and Take N from dictionary value in C#
- Predicate inside a Where<> query not hit?
- Sharepoint 2010 listitem to linq 2 sharepoint spmetal class
- xml linq retrieving elements in same node
- Array of array combinations
- LINQ - "Index was outside the bounds of the array."
- Does foreach execute the query only once?
- Return different objects with one LINQtoSQL statement
- Creating JProperty from Dictionary with LINQ in JSON.NET
- List object ToDictionary <object, List<object>>
- Finding record using LINQ WCF Ado.net "Does not match return type"