开发者

Extracting a list of objects that exist in another list objects using linq

开发者 https://www.devze.com 2022-12-20 12:26 出处:网络
Lin开发者_StackOverflow社区q noob here. I have IList<Product> ApplicableProducts and a IList<Product> CurrentProducts.

Lin开发者_StackOverflow社区q 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


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));


Can be done without LINQ. Here:

var identical = applicableProducts.Intersect(currentProducts);

You may also want to supply your own IEqualityComparer.

0

精彩评论

暂无评论...
验证码 换一张
取 消