Compare lists and return common objects using LINQ?

From a comment to another answer: Intersect expects both lists to be of same type. What if they are not of same type and what if property name is different in both lists? How do I use SelectMany here In this case, you may wish to utilize Join Such as var result = from item1 in list1 join item2 in list2 on item1.

Foo equals item2. Bar select new { item1, item2 }; // select whatever you need in the resultset.

From a comment to another answer: Intersect expects both lists to be of same type. What if they are not of same type and what if property name is different in both lists? How do I use SelectMany here In this case, you may wish to utilize Join.

Such as var result = from item1 in list1 join item2 in list2 on item1. Foo equals item2. Bar select new { item1, item2 }; // select whatever you need in the resultset.

Ideally, make your Customer class override GetHashCode and Equals (better yet, implement IEquatable). Then you can just use: var customerVendors = custList. Intersect(vendorList); Otherwise, you'll to implement an IEqualityComparer to compare customers for equality (e.g.By name, but you may choose other comparisons) and then use: var customerVendors = custList.

Intersect(vendorList, new CustomerComparer()); Note that these will both return IEnumerable, which will be lazily evaluated. Sometimes that's what you want, but if you really need a List, just call ToList() at the end, e.g. Var customerVendors = custList. Intersect(vendorList).ToList().

If your Customer class implements a "value like" Equals() you can just do: var inbothLists = custList. Intersect(vendorList).ToList(); Otherwise you can use the overload that passes a custom IEQualityComparer that you can implement in a separate class (i.e. CustomerComparer) : var inbothLists = custList.

Intersect(vendorList, new CustomerComparer()) .ToList().

Intersect expects both lists to be of same type. What if they are not of same type and what if property name is different in both lists? How do I use SelectMany here?

– Asdfg Apr 18 at 18:29 @Asdfg: Then you're talking about a different question, to be honest... and you'd use a Join. – Jon Skeet Apr 18 at 18:30.

Var commonList = custList. Intersect(vendorList); All you need is IEqualityComparer to compare based on Name.

Var both = (from a in custList join be in vendorList on a. Name equals b. Name select a).

Otherwise, you'll to implement an IEqualityComparer to compare customers for equality (e.g. Note that these will both return IEnumerable, which will be lazily evaluated. Sometimes that's what you want, but if you really need a List, just call ToList() at the end, e.g.

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