如何对通用列表中的 2 个值进行 GroupBy,VB.NET?
我在 Framework 2.0 上使用 VB.NET。我希望快速对通用列表进行分组<>由两个属性。在本示例中,假设我有一个 Order 类型的列表,其中包含 CustomerId、ProductId 和 ProductCount 属性。如何在 VB.NET 中获得按 CustomerId 和 ProductId 分组的 ProductCounts 的平均值?
不幸的是,我无法在数据库级别执行此操作(这很容易)。另外,我无法在 Framework 2.0 上使用 LINQ 或 Lambada。所以我需要在 VB.net 的应用程序级别执行此操作。该列表按 CustomerId 和 ProductId 排序返回给我,所以我想通过几个循环我应该能够创建平均值,但必须有一个更干净的方法来做到这一点?
I'm using VB.NET on Framework 2.0. I'm looking to quickly group a Generic List<> by two properties. For the sake of this example lets say I have a List of an Order type with properties of CustomerId, ProductId, and ProductCount. How would I get the average of ProductCounts grouped by CustomerId and ProductId in VB.NET ?
Unfortunatly I cannot do this at DB level (which would have been easy). Also I cannot use LINQ or Lambada as Im on Framewwork 2.0. So I need to do this at application level in VB.net. The list is returned to me sorted by CustomerId and ProductId so I guess with a few loop sI should be able to create a average value for but there must be a cleaner way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此类问题是为什么在 C# 和 VB.NET 中引入 LINQ 的一个很好的例子。在 .NET 3.5 及更高版本中,LINQ 提供了您正在寻找的“更干净的方法”来解决此问题。
不幸的是,由于您使用的是 .NET 2.0,因此您将或多或少地以“手动”方式解决问题。但是,您仍然可以通过将您正在寻找的功能封装到定义良好的类和方法中来编写干净的代码。这是 LINQ 的优点之一(但不是唯一的),即它以干净、声明的方式封装了您期望的功能。
下面是一些可以帮助您入门的示例代码:
将数据插入到封装良好的类中的好处是使用的代码非常简洁且易于理解。此外,由于您已经将数据聚合到
AggregateItem
类中,因此您可以使用更多方法轻松扩展该类,例如GetSum()
或GetMax()< /代码>。
显然,您可以继续这条抽象路径,以更好地重用代码,但我认为这为您提供了一个良好的开端。
This type of problem is a great example of why LINQ was introduced in C# and VB.NET. In .NET 3.5 and above, LINQ provides the "cleaner way" that you are looking for to solve this problem.
Unfortunately, because you are using .NET 2.0, you'll have solve the problem in a more or less "manual" fashion. However, you can still write clean code by encapulating the functionality you are looking for into well-defined classes and methods. This is one (but not the only) of the benefits of LINQ, i.e. that it encapsulates the functionality you expect in a clean, declarative manner.
Here's some sample code to get you started:
The nice thing about inserting your data into well-encapsulated classes is that the consuming code is very succinct and easy to understand. Also, since you already have your data aggregated into an
AggregateItem
class, you could easily extend that class with more methods such asGetSum()
orGetMax()
.Obviously, you could continue on this path of abstraction to get better re-use out of your code, but I would think that this gives you a good start.