首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

C#中怎样用LINQ话语查询出DataTable中的最大值

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

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();

热点排行