2013-04-11 65 views
1

说我有这个表如何在LINQ中获得单列中多列的最大值?

MyTable的

ID         NumValue1       NumValue2       NumValue3       NumValue4       NumValue5
        12.17                             23.97                                                                 0.07                              

如何获得这23.97使用LINQ语法此单列最大值?

在SQL(MS SQL),我可以写这样的事:

SELECT MaxNum = case 
when MyTable.NumValue1 is not null and 
(MyTable.NumValue1 >= MyTable.NumValue2 or MyTable.NumValue2 is null) and 
(MyTable.NumValue1 >= MyTable.NumValue3 or MyTable.NumValue3 is null) and 
(MyTable.NumValue1 >= MyTable.NumValue4 or MyTable.NumValue4 is null) and 
(MyTable.NumValue1 >= MyTable.NumValue5 or MyTable.NumValue5 is null) 
then MyTable.NumValue1 
when MyTable.NumValue2 is not null and 
(MyTable.NumValue2 >= MyTable.NumValue1 or MyTable.NumValue1 is null) and 
(MyTable.NumValue2 >= MyTable.NumValue3 or MyTable.NumValue3 is null) and 
(MyTable.NumValue2 >= MyTable.NumValue4 or MyTable.NumValue4 is null) and 
(MyTable.NumValue2 >= MyTable.NumValue5 or MyTable.NumValue5 is null) 
then MyTable.NumValue2 
when MyTable.NumValue3 is not null and 
(MyTable.NumValue3 >= MyTable.NumValue1 or MyTable.NumValue1 is null) and 
(MyTable.NumValue3 >= MyTable.NumValue2 or MyTable.NumValue2 is null) and 
(MyTable.NumValue3 >= MyTable.NumValue4 or MyTable.NumValue4 is null) and 
(MyTable.NumValue3 >= MyTable.NumValue5 or MyTable.NumValue5 is null) 
then MyTable.NumValue3 
when MyTable.NumValue4 is not null and 
(MyTable.NumValue4 >= MyTable.NumValue1 or MyTable.NumValue1 is null) and 
(MyTable.NumValue4 >= MyTable.NumValue2 or MyTable.NumValue2 is null) and 
(MyTable.NumValue4 >= MyTable.NumValue3 or MyTable.NumValue3 is null) and 
(MyTable.NumValue4 >= MyTable.NumValue5 or MyTable.NumValue5 is null) 
then MyTable.NumValue4 
when MyTable.NumValue5 is not null and 
(MyTable.NumValue5 >= MyTable.NumValue1 or MyTable.NumValue1 is null) and 
(MyTable.NumValue5 >= MyTable.NumValue2 or MyTable.NumValue2 is null) and 
(MyTable.NumValue5 >= MyTable.NumValue3 or MyTable.NumValue3 is null) and 
(MyTable.NumValue5 >= MyTable.NumValue4 or MyTable.NumValue4 is null) 
then MyTable.NumValue5 
else null 
end 
FROM MyTable WHERE MyTable.ID=1; 


谢谢。

回答

1

鉴于你DataTable看起来像这样:

var t = new DataTable(); 
t.Columns.Add("ID", typeof(Int32)); 
t.Columns.Add("NumValue1", typeof(decimal)); 
t.Columns.Add("NumValue2", typeof(decimal)); 
t.Columns.Add("NumValue3", typeof(decimal)); 
t.Columns.Add("NumValue4", typeof(decimal)); 
t.Columns.Add("NumValue5", typeof(decimal)); 

和你的行看起来是这样的:

// get all values: 
var max = row.ItemArray 
      // only values that are not DBNull: 
      .Where(o => o != DBNull.Value) 
        // convert to decimal and get the max value: 
        .Max(o => Convert.ToDecimal(o)); 

max

var row = t.Rows.Add(new object[] {1, 
            12.17m, 
            23.97m, 
            DBNull.Value, 
            0.07m, 
            DBNull.Value}); 

你可以用这个查询得到最大的价值现在是23.97

+0

Thanks.I'm非常新的LINQ。我相信这是拉姆达表达。这在正常的LINQ中看起来如何?再次感谢。 – mADy1270 2013-04-11 09:47:02

+0

@ mADy1270你的意思是在查询语法?你可以使用'var max =(从row.ItemArray中的o,o!= DBNull.Value选择Convert.ToDecimal(o))。Max()' – sloth 2013-04-11 09:52:57

+0

是的。这是我正在寻找的。但看着这个,它看起来像评估行中的所有列。如果我只是想从NumValue1,NumValue2和NumValue3列中获取MAX,而不是列NumValue4和NumValue5,那么该怎么办?谢谢 – mADy1270 2013-04-12 01:12:20

相关问题