Linq Min()的非零值

时间:2013-10-30 11:07:00

标签: c# linq

我有一个包含OneStopPrice; MultiStopPrice; NonStopPrice列的表格。我希望所有3列都有Min()。但如果任何列的Min()为0,则必须忽略它。

OneStop | NonStop | MultiStop
-----------------------------
233.23  | 0.0     | 355.23

这里必须将NonStop视为最高值,以便忽略它。

任何人都有使用LinQ

的想法

我使用了如下查询:

var data = odyResults.GroupBy(logEle => new { logEle.Air.FromCity, logEle.Air.ToCity }, (key, group) => new{
       FromCity = key.FromCity,
       ToCity = key.ToCity,
       OneStopPrice = group.Min(x=>x.Air.OneStopPrice),
       MultiStopPrice = group.Min(x => x.Air.MultiStopPrice),
       NonStopPrice = group.Min(x => x.Air.NonStopPrice),
       FromToCityCount = group.Count()
      });

1 个答案:

答案 0 :(得分:2)

只需过滤掉零:

NonStopPrice = group.Where(x => x.Air.NonStopPrice > 0)
                    .Min(x => x.Air.NonStopPrice),