C# 在没有 linq 的情况下在内存中查询对象
我们仍在使用 .Net Framework 2.0 / VS 2005,所以我没有 LINQ。如果我不想使用 穷人的 LINQ 解决方案,那么什么是能够在内存中查询字典中的自定义对象的其他替代方法?
We are still using .Net Framework 2.0 / VS 2005 so i do not have LINQ. If i don't want to go with the poor man's LINQ solution, what are some other alternatives for being able to query in memory custom objects in a dictionary?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不确定你的穷人的 LINQ 解决方案之一是否是 LINQBridge 但我使用了它在我们真正切换到 .NET 3.5 和真正的交易之前,它似乎工作了几个星期没问题。
I'm not sure if one of your poor man's LINQ solution is LINQBridge but I used it for a few weeks and it seemed to be working okay before we actually switched to .NET 3.5 and the real deal.
尽管您没有提供有关“查询”含义的太多信息,但
Dictionary
似乎是一个不错的选择。您只是想根据某个键值检索数据吗?获取总项目数?根据某些条件求和或求平均值?您确实需要提供更多信息才能获得更好的答案。Dictionary<T>
would seem like a good choice, although you haven't provided much information about what you mean by "query." Are you just looking to retrieve data based on some key value? Get a count of total items? Do a sum or average based on some condition? You really need to give more information to get a better answer.要详细说明 Chesso 所说的内容,您必须像 LINQ 一样迭代循环......
例如:
To elaborate on what Chesso said, you'll have to iterate the loop just like LINQ does...
for example:
我不知道谓词委托,这似乎正是我正在寻找的。就我正在查询的上下文而言:
假设我有一个对象 X,其属性 A(名称,保证是唯一的)和 B(年龄)
1)我在字典中有一系列对象,其键为 Property给定对象的 A,当然值就是对象本身。
现在我想检索该字典中满足 B 特定标准的所有对象,例如年龄 > 年龄。 20.
我可以将字典的所有值添加到列表中,然后在其上调用 .FindAll,并传入委托。我可以创建一个匿名委托来执行此操作,但我会多次重复使用它。如何动态指定委托方法的年龄标准?唯一的选择是将 Predicate 方法封装在一个类中,然后使用我的条件作为实例变量创建该类的新实例吗?
I was not aware of the Predicate delegate, that seems to be pretty much what i was looking for. As far as the context for which i'm querying:
Say i have a object X with properties A (name, guaranteed to be unique) and B (age)
1) I have a series of objects in a dictionary whose keys are say Property A of a given object, and of course the value is the object iself.
Now i want to retrieve all objects in this dictionary which meet a certain criteria of B, say age > 20.
I can add all the values of the dictionary into a list then call the .FindAll on it, passing in a delegate. I can create an anonymous delegate to do this, but say i will reuse this many times. How can i dynamically specify an age criteria for the delegate method? Would the only choice be to encapsulate the Predicate method in a class, then create a new instance of that class with my criteria as an instance variable?