0

Linq noob here.

I have IList<Product> ApplicableProducts and a IList<Product> CurrentProducts.

I need to return a new IList<Product> of all CurrentProducts that exist in Applicable products.

I understand I need to be working with .Contains and .Any but getting a little lost.

Any tips appreciated

2 Answers 2

4

http://msdn.microsoft.com/en-us/library/system.linq.enumerable.intersect.aspx

var intersection = list1.Intersect(list2);

or

var intersection = list1.Where(i => list2.Contains(i));
Sign up to request clarification or add additional context in comments.

2 Comments

Calling Intersect should be faster for large collections. (It uses a hashtable)
@SLaks: Yeah, I was going to speculate on that, but I wasn't sure. Thanks.
2

Can be done without LINQ. Here:

var identical = applicableProducts.Intersect(currentProducts);

You may also want to supply your own IEqualityComparer.

1 Comment

That is using linq. Intersect is defined in the System.Linq namespace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.