2013-10-30 46 views
1

我有一個列表OneStopPrice; MultiStopPrice; NonStopPrice列表。我希望所有3列都有Min()。但是如果任何列的Min()爲0,則必須忽略它。Linq Min()的非零值

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() 
     }); 
+0

你說的*忽略*是什麼意思?屬性不應該出現在匿名對象中? –

+4

'group.Where(c => c.OneStopPrice!= 0).Min(c => c.OneStopPrice)'?不知道,你正在嘗試做什麼,但。 – Leri

+0

忽略意味着我希望接近最低值。意味着非零值的Min()。 –

回答

2

就過濾掉零:

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

它給我錯誤 - 序列包含沒有元素 –

+0

這是因爲'group.Where(x => x.Air.NonStopPrice> 0)'是空的,你應該相應地處理這種情況。 –

+0

你可以試試這個:'group.Select(x => x.Air.NonStopPrice).Where(p => p> 0).DefaultIfEmpty()。Min()' –