1

LINQPad用来评估我的 linq 查询。我的查询是这样的:

from o in MyTableFirst
join p in MyTableSecond on o.TheName equals p.TheName
where p.TheName == "CBA-123" && !p.Removed && 
   (o.ReturnPeriod ==100 || o.ReturnPeriod ==10)
select new {
   HMax1 = o.MaxValue1,
   HMax2 = o.MaxValue2,
   HMax3 = o.MaxValue3
}


此查询可以返回 0 或一些行数。

在 LINQPad 中,它返回给我的是这样的:

HMax1    HMax2    HMax3
21.1         null          22.5
null          24.6 11.5

现在,我将如何获得这些返回行和列的最大值?
我期待24.6的回报。

谢谢你

4

2 回答 2

2

这个怎么样:

(from o in db.MyTableFirsts
 join p in db.MyTableSeconds on o.TheName equals p.TheName
 where p.TheName == "CBA-123" && !p.Removed &&
 (o.ReturnPeriod == 100 || o.ReturnPeriod == 10)
  select new
  {
    Maximum = Math.Max(
       Math.Max((float)(o.MaxValue1 ?? 0), (float)(o.MaxValue2 ?? 0)),
       (float)(o.MaxValue3 ?? 0)
    )
  }).OrderByDescending(o => o.Maximum).FirstOrDefault();

或者代替 .OrderByDescending(o => o.Maximum).FirstOrDefault(),您可以使用 .Max(o => o)

于 2013-04-12T09:41:38.890 回答
1

尝试这个:

(
 from o in MyTableFirst
 join p in MyTableSecond on o.TheName equals p.TheName
 where p.TheName == "CBA-123" && !p.Removed && 
 (o.Level ==100 || o.Level ==10)

 //combine all of the numbers into one list
 let listOfNumbers = new List<double?>{o.MaxValue1,o.MaxValue2,o.MaxValue3}

 //select the list
 select listOfNumbers
)
.SelectMany(c => c) //combine all the lists into one big list
.Max(c => c) //take the highst number
于 2013-04-12T06:44:40.117 回答