[英]How to find all items in a sorted List<T> using .NET 2.0?
How can I find all items in a sorted collection? 如何找到已排序集合中的所有项目?
Basically I have a List<T>
collection (large one). 基本上我有一个
List<T>
集合(大一个)。 First I want to sort it so that the subsequent calls for finding all matches could be performed as fast as possible. 首先,我想对它进行排序,以便可以尽快执行随后的查找所有匹配项的调用。 i would like to use some build-in mechanisms rather than performing manual searches.
我想使用一些内置机制,而不是执行手动搜索。
I initially used FindAll
but that probably will enumerate the entire collection and I need to speed it up. 我最初使用的是
FindAll
但是可能会枚举整个集合,因此我需要加快速度。
It has to be done under .NET 2.0 它必须在.NET 2.0下完成
EDIT 编辑
List<Custom>
List<Custom>
Custom is a class with 3 public
fields (it is a container) Custom是具有3个
public
字段的类(它是一个容器)
Then I sort it: 然后我对其进行排序:
collection.Sort((x, y) => x.Id.CompareTo(y.Id));
// Id is a Guid
// Id是
Guid
Now I need to find all items in a collection by Id without enumerating all items in a collection. 现在,我需要按ID查找集合中的所有项目,而无需枚举集合中的所有项目。
IEqualityComparer<T>
and IEquatable<T>
interfaces have been available since .NET 2.0 and this framework version introduced generic dictionaries : Dictionary<TKey, TValue>
. 从.NET 2.0开始,
IEqualityComparer<T>
和IEquatable<T>
接口就已经可用,并且此框架版本引入了通用词典 : Dictionary<TKey, TValue>
。
What you can do is indexing . 您可以做的是建立索引 。 For example, if you want to get your objects by id in a constant time, in addition to adding them to your list, you can add them to a
Dictionary<Guid, Custom>
. 例如,如果要在固定时间内通过id获取对象,则除了将它们添加到列表中之外,还可以将它们添加到
Dictionary<Guid, Custom>
。
Now you can get objects by id in a constant time: 现在您可以在固定时间内通过id获取对象:
Custom custom = all[someGuid];
This approach can be followed by the rest of your class properties. 其余的类属性都可以使用这种方法。
Why I've talked about IEqualityComparer<T>
and IEquatable<T>
? 为什么我谈论
IEqualityComparer<T>
和IEquatable<T>
? Because if one of the whole properties is another custom class, maybe you'll need to implement one of the so-called interfaces (and/or override Object.Equals
and Object.GetHashCode
) to implement a custom equality, so dictionary keys will be unique. 因为如果整个属性之一是另一个自定义类,则可能需要实现一个所谓的接口(和/或重写
Object.Equals
和Object.GetHashCode
)以实现一个自定义的相等性,因此字典键将是独特。
If you want to combine more than a property, maybe you'll need to define a class to provide one or more properties: 如果您要组合多个属性,则可能需要定义一个类以提供一个或多个属性:
public class Args
{
public CustomA CustomA;
public CustomB CustomB;
}
...and you can implement an IEqualityComparer<T>
for Args
to provide a combined hash code and be able to use Args
as dictionary key. ...并且您可以为
Args
实现IEqualityComparer<T>
,以提供组合的哈希码,并能够将Args
用作字典键。 This would be like looking for an object which matches all provided properties in a given Args
instance... (ie like using an &&
operator in a where clause...). 这就像在一个给定的
Args
实例中寻找匹配所有提供的属性的对象一样(例如,在where子句中使用&&
运算符...)。
Did you try Sort from the Collections namespace? 您是否尝试过从Collections名称空间排序? Try providing a Comparer.
尝试提供比较器。
List.Sort Method () .NET Framework 2.0 List.Sort方法().NET Framework 2.0
Why not use a 2D array instead of sorted list, where the first dimension is the GUIDs? 为什么不使用2D数组而不是排序列表(第一个维度是GUID)呢?
OR 要么
Sort the list, use IndexOf to get the first index of your GUID. 对列表进行排序,使用IndexOf获取GUID的第一个索引。 Then, use RemoveRange to remove all items before the index you got.
然后,使用RemoveRange删除索引之前的所有项目。 And lastly, use Find on the remaining range to get the first GUID that doesn't match your GUID.
最后,在剩余范围内使用“查找”以获取第一个与您的GUID不匹配的GUID。
See here: LINQ support on .NET 2.0 参见此处: .NET 2.0上的LINQ支持
The easiest way I can think of is to use Linq: 我能想到的最简单的方法是使用Linq:
wrong way 错误道
List<Order> objListOrder = new List<Order>();
GetOrderList(objListOrder); // fill list of orders
Right way 正确的路
List<Order> SortedList = objListOrder.OrderBy(o=>o.OrderDate).ToList();
Use LINQ with .NET Framework 2.0 将LINQ与.NET Framework 2.0结合使用
LINQBridge (from the author of the excellent LINQPad) is a small 60KB assembly that when combined with the multi-targeting capabilities of Visual Studio 2008 gives the ability to write code which does LINQ to Objects (not XML or SQL) but runs fine on a machine with just the .NET Framework 2.0 installed. LINQBridge (来自出色的LINQPad的作者)是一个60KB的小型程序集,当与Visual Studio 2008的多目标功能结合使用时,便可以编写对对象(不是XML或SQL)执行LINQ的代码,但可以在仅安装.NET Framework 2.0的计算机。 Very clever
非常聪明
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.