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;
謝謝。
Thanks.I'm非常新的LINQ。我相信這是拉姆達表達。這在正常的LINQ中看起來如何?再次感謝。 – mADy1270 2013-04-11 09:47:02
@ mADy1270你的意思是在查詢語法?你可以使用'var max =(從row.ItemArray中的o,o!= DBNull.Value選擇Convert.ToDecimal(o))。Max()' – sloth 2013-04-11 09:52:57
是的。這是我正在尋找的。但看着這個,它看起來像評估行中的所有列。如果我只是想從NumValue1,NumValue2和NumValue3列中獲取MAX,而不是列NumValue4和NumValue5,那麼該怎麼辦?謝謝 – mADy1270 2013-04-12 01:12:20