Return Max value with LINQ Query in VB.NET?

牧云@^-^@ 提交于 2020-01-23 12:24:10

问题


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.


回答1:


You can use the DataSet Extensions for Linq to query a datatable.

The following query should help.

Dim query = _
    From value In dt.AsEnumerable() _
    Select value(Of Double)("ColumnName").Max();

If you don't now in which column will hold the maximum you could use: (C#)

var result = from r in table.AsEnumerable()
             select r.ItemArray.Max();


来源:https://stackoverflow.com/questions/7995526/return-max-value-with-linq-query-in-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!