Using LINQ to Objects to find items in one collection that do not match another?

This is almost the same as some other examples but less code: employees. Except(employees. Join(managers, e => e.Id, m => m.

EmployeeId, (e, m) => e)) It's not any simpler than employees. Where(e =>!managers. Any(m => m.

EmployeeId == e. Id)) or your original syntax, however.

This is almost the same as some other examples but less code: employees. Except(employees. Join(managers, e => e.Id, m => m.

EmployeeId, (e, m) => e)); It's not any simpler than employees. Where(e =>!managers. Any(m => m.

EmployeeId == e. Id)) or your original syntax, however.

Actually I like this better than the other solutions - I find its meaning clearer. I rewrote the join in query syntax (see the revised sample code in my question) out of personal preference. Thank you!

– TrueWill Oct 31 '09 at 17:17.

This method returns items in a set that are not in /// another set of a different type /// /// /// /// /// /// /// /// /// public static IEnumerable Except( this IEnumerable items, IEnumerable other, Func getItemKey, Func getOtherKey) { return from item in items join otherItem in other on getItemKey(item) equals getOtherKey(otherItem) into tempItems from temp in tempItems.DefaultIfEmpty() where ReferenceEquals(null, temp) || temp. Equals(default(TOther)) select item; } I don't remember where I found this method.

1 - Nice. I modified this slightly and incorporated it in my question. I want to see what others come up with, though.

Thanks! – TrueWill Oct 30 '09 at 14:53.

Have a look at the Except() LINQ function. It does exactly what you need.

The except function only works with 2 sets of the same object type, but would not direclty apply to his example with employees and managers. Therefore the overloaded method in my answer. – cdonner Oct 31 '09 at 0:43.

Var nonManagers = ( from e1 in employees select e1 ). Except( from m in managers from e2 in employees where m. EmployeeId == e2.Id select e2 ).

1. Elegant and works correctly. – TrueWill Oct 30 '09 at 17:14 1 Thanks.

Originally found it here : rsanidad.wordpress. Com/2007/10/16/linq-except-and-intersect – Partha Choudhury Oct 30 '09 at 18:04.

Var nonmanagers = employees. Select(e => e. Id) .

Except(managers. Select(m => m. EmployeeId)) .

Select(id => employees. Single(e => e. Id == id)).

1 There is no guarantee that the EmployeeId will match the employee index in the array... – Thomas Levesque Oct 30 '09 at 13:04 Nice idea - I didn't think of selecting the IDs so that Except with the default equality comparer would compare integers. However Mr. Levesque is correct, and I've updated the example to reflect this. Can you provide an example that correctly returns the employees?

– TrueWill Oct 30 '09 at 14:38 Ah you're right. Answer has been updated. – gWiz Oct 30 '09 at 19:39 (I deleted my previous comment - gWiz is right; this will work.) – TrueWill Oct 30 '09 at 17:22.

I want to find all items in one collection that do not match another collection. The collections are not of the same type, though; I want to write a lambda expression to specify equality. The above works, and will return Employee Bill (#10).

It does not ...

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions