从列表中删除项目的有效方法 [英] Efficient way to Remove Item from the List

查看:147
本文介绍了从列表中删除项目的有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有两个无序列表-result1和result2如下所示.
通过使用下面的代码,我可以填充result1,但不包括这两个列表之间的公共项.
此代码非常适合小尺寸列表.但是,在实际情况下,我有两个包含数百万个项目的列表.使用类似的方法将一个列表中的公共值消除到另一个列表中会占用太多时间.
因此,我想知道是否有一种更有效的方法来处理这种情况.

列表< int [] >  result1 =  List< int [ ] >  {  int  [] { 1  2  3 },   int  [] { 4  5  6 }};
List< int [] >  result2 =  List< int []   2  3 }, int  [] { 8  9 }};

result2.ForEach(t = >  result1.RemoveAll(z = >  z.OrderBy(k = > k).SequenceEqual(t.OrderBy(k => k)))); 

解决方案

尝试以下操作:http://stackoverflow.com/questions/3047657/linq-to-sql-in-and-not -in [ ^ ]

或这样:如何:找到两个列表(LINQ)之间的集合差异 [ ^ ]


So, I have a two un-ordered list - result1 and result2 as listed below.
By using a code below I am able to populate result1 excluding the common items between those two lists.
This code works perfectly for the small size list. However, in real scenario I have two lists with millions of item on it. Using the similar way of eliminating the common values from one list into the other list is taking way too much time.
So, I was wondering if there is an more efficient way to deal with this kind of scenario.

List<int[]> result1 = new List<int[]> { new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 } };
List<int[]> result2 = new List<int[]> { new int[] { 2, 1, 3 }, new int[] { 7, 8, 9 } };

result2.ForEach(t => result1.RemoveAll(z => z.OrderBy(k=>k).SequenceEqual(t.OrderBy(k=>k))));

解决方案

Try this: http://stackoverflow.com/questions/3047657/linq-to-sql-in-and-not-in[^]

Or this: How to: Find the Set Difference Between Two Lists (LINQ)[^]


这篇关于从列表中删除项目的有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆