什么是从另一个删除项目的一个数组的最快方法? [英] What is the quickest way to remove one array of items from another?

查看:142
本文介绍了什么是从另一个删除项目的一个数组的最快方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有弦的两个数组:

 的String [] =所有新的String [] {A,B,C,D}

 字符串[] =采取新的String [] {A,B}
 

我要生成一个新的字符串数组 C D 所有 - 采取

在任何快捷方式 .NET 3.5 这样做没有人工循环,并创造新的列表?

解决方案

  VAR仍然= all.Except(取);
 

请注意,这确实的没有的返回数组。但是,你要问自己,如果你真的需要一个数组或者IEnumerable的是比较合适的(提示:它几乎总是)。如果你真的需要一个数组,你可以叫 .ToArray()来得到它。

在这种情况下,有可能是一个很大的性能优势的没有的使用数组的时候了。想想你有你的所有收集a到D和A和B在您的取的集合。在这一点上,保持变量不包含的任意的数据呢。相反,它认为的知道如何告诉你一个对象的哪些数据将在那里当你问吧。如果你从来没有真正需要这个变量,你从来没有任何工作来计算属于它什么物品。

I have two arrays of strings:

 string[] all = new string[]{"a", "b", "c", "d"}

 string[] taken = new string[]{"a", "b"}

I want to generate a new string array with c and d which is all - taken.

Any quick way in .net 3.5 to do this without a manual loop and creating new lists?

解决方案

var remains = all.Except(taken);

Note that this does not return an array. But you need to ask yourself if you really need an array or if IEnumerable is more appropriate (hint: it almost always is). If you really need an array, you can just call .ToArray() to get it.

In this case, there may be a big performance advantage to not using an array right away. Consider you have "a" through "d" in your "all" collection, and "a" and "b" in your "taken" collection. At this point, the "remains" variable doesn't contain any data yet. Instead, it's an object that knows how to tell you what data will be there when you ask it. If you never actually need that variable, you never did any work to calculate what items belong in it.

这篇关于什么是从另一个删除项目的一个数组的最快方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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