Linq List comparing
Linq is great, but it always seems to c开发者_如何学Confuse me a bit.
This is my latest confusion:
Say I have two List<String>
objects. We will call them sourceList
and destList
.
I need a way to find the list of strings that are in sourceList and not in destList AND find the list of strings that are in destList and not in SourceList.
This is a bit confusing so here is the example:
sourceList destList Orange Apple Apple Grape Grape Kiwi Banana
So the first result I am looking for would be a list with Orange in it. The second result would the a list with Kiwi and Banana in it.
Any idea how to do that with Linq?
sourceList.Except(destList)
Should get a difference of source and dest. You can also do the reverse and combine.
I was just doing this earlier today actually. As sukru said this code should do it for you:
List<string> firstResultList = sourceList.Except(destList);
List<string> secondResultList = destList.Except(sourceList);
firstResultList will have Orange in it and secondResultList will have Kiwi and Banana.
精彩评论