Find items from a list which exist in another list
Solution 1
What you want to do is Join
the two sequences. LINQ has a Join
operator that does exactly that:
List<PropX> first;
List<PropA> second;
var query = from firstItem in first
join secondItem in second
on firstItem.b equals secondItem.b
select firstItem;
Note that the Join
operator in LINQ is also written to perform this operation quite a bit more efficiently than the naive implementations that would do a linear search through the second collection for each item.
Solution 2
ListA.Where(a => ListX.Any(x => x.b == a.b))
Solution 3
var commonNumbers = first.Intersect(second);
This will give you the common values between two lists, a much faster and cleaner approach than join or other Lambda expressions.
Just try it.
Source : MSDN
Solution 4
Well all above will not work if you have multiple parameters, So I think this is the best way to do it.
For example: Find not matched items from pets and pets2 .
var notMatchedpets = pets
.Where(p2 => !pets2
.Any(p1 => p1.Name == p2.Name && p1.age == p2.age))
.ToList();
Related videos on Youtube
donstack
Updated on July 09, 2022Comments
-
donstack almost 2 years
I have a
List<PropA>
PropA { int a; int b; }
and another
List<PropX>
PropX { int a; int b; }
Now i have to find items from
List<PropX>
which exist inList<PropA>
matching b property using lambda or LINQ.-
walther about 11 yearsAnd what have you tried so far? This question has been answered way too many times already...
-
Dave Bish about 11 years
-
Servy about 11 years@David Perhaps they each have different fields that he hasn't shown, or they have a different set of methods even though they have the same values, or any number of other reasons.
-
David about 11 yearsgot it! I thought both a & b should match. It is my mistake.
-
-
Malcolm O'Hare about 11 yearsThere is also the other way of writing this...
first.Join(second, f => f.b, s => s.b, (fir, sec) => fir);
-
Servy about 11 years@MalcolmO'Hare Yes, and both are completely identical. This will compile into that. I've found that most people prefer
Join
to be written out using query syntax over method syntax, even more so than any of the other query methods. -
Servy about 11 yearsWell, I ran a simple test and the results of a
Join
were substantially faster. ideone.com/xW1CnL . You'll need to run the code yourself to use a larger data set. -
Adrian Godong about 11 yearsYep, I forgot to run the query. It seems that for a small enough data set (less than 500 items), the instantiation cost of join outweighs the performance gain.
-
Adrian Godong about 11 yearsIf performance is out of the question, then I would take code simplicity. This would be a subjective and personal preference.
-
Adrian Godong about 11 yearsDepending on how the business logic and input data goes, one may want to include a Distinct() call to remove duplicate values returned by the join.
-
Servy about 11 yearsAnd in this case the operation being performed is fundamentally a join. That is the semantic meaning of this code, so it makes the most sense to use that exact operator rather than using
Where
to implement your ownJoin
.Join
does a better job of conveying the meaning to the reader as a result. -
Daniel over 8 yearsThis would only work if
T
was the same type for bothIEnumerable<T>
collections, which is not the case in the question asked. -
Simo Kivistö about 5 years+1, This is one of those cases where you open the question based on the title and this is the perfect answer to that part although it doesn't answer the actual content of the question.
-
Ambuj about 5 yearsThanks Simo Kivisto