在 VB.NET 中使用 LINQ 查询返回最大值?
我有一个方法,它接受 id、开始日期和结束日期作为参数。它将返回数据行,每行数据对应于日期范围内的星期几。这些行都是双打的。将其返回到 DataTable 后,我需要能够在 VB.NET 中使用 LINQ 返回最大值。我怎样才能实现这个目标?这是初始设置吗?
Dim dt as DataTable = GetMaximumValue(1,"10/23/2011","11/23"/2011")
'Do linq query here to return the maximum value
另一种选择是只返回给定 id 的最大值,这会更容易实现,因此该方法如下所示:
Dim dt as DataTable = GetMaximumValue(1)
'Do linq query here to return maximum value
重要
如果我想查询 DataRow 而不是DataTable 和列名称不一样,它们类似于 MaxForMon
、MaxForTue
、MaxForWed` 等...并且我需要取最大值(无论是是 MaxForMon, MaxForTue,或其他一些专栏)。对此的一个选项是为存储过程中返回的列指定别名吗?我想到的另一件事是,我可能应该在 T-SQL 中处理它,而不是在 LINQ 中执行此操作。
I have a method that takes in an id, a start date, and and end date as parameters. It will return rows of data each corresponding to the day of the week that fall between the date range. The rows are all doubles. After returning it into a DataTable, I need to be able to use LINQ in VB.NET to return the maximum value. How can I achieve this? Here is the initial setup?
Dim dt as DataTable = GetMaximumValue(1,"10/23/2011","11/23"/2011")
'Do linq query here to return the maximum value
The other alternative is to just return a Maximum value just given an id which would be slightly easier to implement, so the method would look like this:
Dim dt as DataTable = GetMaximumValue(1)
'Do linq query here to return maximum value
Important
What if I want to query against a DataRow instead of a DataTable and the column names are not the same, they are something like MaxForMon
, MaxForTue
, MaxForWed`, etc... and I need to take the Maximum value (whether it is MaxForMon, MaxForTue, or some other column). Would an option for this be to alias the column returned in the stored proc? The other thing I thought about was instead of doing this in LINQ, I probably should just handle it in the T-SQL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用Linq 数据集扩展来查询数据表。
以下查询应该有所帮助。
如果您现在不知道哪一列将保存您可以使用的最大值:(C#)
You can use the DataSet Extensions for Linq to query a datatable.
The following query should help.
If you don't now in which column will hold the maximum you could use: (C#)