日期:2014-05-20  浏览次数:21165 次

C#中怎样用LINQ语句查询出DataTable中的最大值?
我的价格表(PriceTable)已经在数据库中查询到了数据,其内容为:

id SalesVolume UnitPrice  

1 5 7.9  

2 8 4.3  

3 4 5.8  

4 10 12.1  

5 12 3.5  

6 6 14  

7 9 7  

8 11 8.8  

9 3 10.5  

10 8 3.9  



请教各位高手能否使用一个LINQ语句查询出PriceTable表的SalesVolume和UnitPrice 列的最大值?



------解决方案--------------------
C# code

int maxSV=dt.AsEnumerable().Select(t=>t.Field<int>("SalesVolume")).Max();

int maxUP=dt.AsEnumerable().Select(t=>t.Field<int>("UnitPrice")).Max();

------解决方案--------------------
var query = (dt.AsEnumerable().Select(t=>t.Field<int>("SalesVolume")).Max()).Union (dt.AsEnumerable().Select(t=>t.Field<int>("UnitPrice")).Max())
------解决方案--------------------
int minUP=dt.AsEnumerable().Where(t=>t.Field<int?>("UnitPrice")!=null).(t=>t.Field<int>("UnitPrice")).Min();