如何在没有 Linq 的情况下从列表中获取一些对象?
我正在运行 C# 框架 2.0,我想从列表中获取一些数据? 该列表是List<>。 如何在不循环和手动比较 List<> 的每个元素的情况下做到这一点?
I am running C# framework 2.0 and I would like to get some of the data from a list? The list is a List<>. How can I do that without looping and doing comparaison manually on each element of the List<>?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果我正确地理解了您的问题,您只需调用
Find()
或FindAll()
方法即可从列表中获取数据项。 例如:If I follow your question correctly, you can just call the
Find()
orFindAll()
methods to get data items out of the list. For example:您可以尝试谓词。 这是我为了说明这一点而编写的代码。 当然,正如您在本例中看到的,您可以将谓词移到调用类之外并对其进行控制。 如果您需要更多选项,这非常有用。 在谓词内部,您可以与对象的所有属性/功能进行多次比较。
You can try Predicate. Here is a code I wrote to illustrate the point. Of course, as you can see in this example, you can move the Predicate outside the calling class and have a control on it. This is useful if you need to have more option with it. Inside the predicate you can do many comparison with all property/function of your object.
如果没有 LINQ,您的主要选择是对列表的每个元素进行循环和比较。 不过,有一些方法可能会对您有所帮助。
List.FindAll()
采用Predicate
委托,并将返回与条件匹配的所有项目。List.CopyTo()
和List.GetRange()
可让您提取一系列元素。 除此之外,在 LINQ 之外您确实无法做太多特定选择的事情。Without LINQ, your main option is to do looping and comparison on each element of the list. There are a few methods that might help you though.
List<T>.FindAll()
takes aPredicate
delegate and will return all items that match the condition.List<T>.CopyTo()
andList<T>.GetRange()
let you extract a range of elements. Other than that, you really can't do much in the way of specific selection outside of LINQ.事实是,即使您最终通过 Find 或 FindAll 使用谓词,它在内部所做的只是循环遍历列表,并运行您的谓词来测试匹配。 就性能而言,您没有获得任何好处,但它确实可以使代码更加简洁。
The truth is, even if you do end up using Predicates through Find or FindAll, all it's doing internally is looping through the list, and running your Predicate to test for a match. Performance wise you're not gaining anything, but it definitely makes for neater code.
不幸的是,List 数据结构需要迭代来查找数据(请注意,上面的 FindAll 方法将在幕后迭代您的集合 - 以防您不惜一切代价试图避免这种情况),除非您知道该数据的索引,否则您可以做这个:
Unfortunately the List data structure requires iteration to find data (note that the FindAll methods above will iterate your collection under the covers - just in case you were trying to avoid that at all costs), unless you know the index of that data then you can do this:
我认为上面的大多数答案都只是提供了替代方法来完成您想要避免的事情。 给定数据结构,无论您选择哪种实现,您/它都必须比较每个项目并挑选出匹配的项目。
根据您的匹配条件,SortedList 可以帮助您避免为每个查询搜索整个列表。 如果您的列表包含您创建的对象,则覆盖 GetHashCode 和 Equals 将会帮助您。
如果您的匹配条件相当复杂,则可能需要完全不同的数据结构,例如 trie对于字符串。
I think most of the answers above are all just providing alternative ways of doing exactly what you're trying to avoid. Given the data structure, no matter what implementation you choose, you/it will have to compare every item and pick out those that match.
Depending on your matching criteria, a SortedList may help you avoid searching the whole list for every query. If your list contains objects you've created, overriding GetHashCode and Equals will help you out.
If you matching criteria is fairly complex, you may need a different data structure altogether, like a trie for strings.