2016-08-11 42 views
0

早些時候,我提出了一個關於如何通過在c#中使用linq進行分組的問題。以下是鏈接。 現在我收到一個錯誤,當我的一個密鑰組爲空。 基本上我我得到C#Linq組多列獲取沒有給定標識符的行

var result = collection.GroupBy(item => new { item.PNO, item.GCode }) 
      .Select(grouping => new 
      { 
       PNO = grouping.Key.PNO, 
       GCode = grouping.Key.GCode,//when this GCode is null, getting error 
       Options = grouping.Select(item => new { Color = item.Color, Size = item.Size, Amount = item.Amount }).ToList() 
      }).ToList(); 

錯誤是一樣的東西:

No row with the given identifier exists[....] 

任何人都可以請建議我一個解決方案?

C# Linq how to use group by to get the desired output

感謝

+0

當我運行了一些樣本數據eith'GCode'空它的工作原理的代碼。請顯示你有什麼數據。 –

+0

基本上我從NHibernate查詢結果中獲取數據。 –

回答

0

當我發現thisthis錯誤搜索。

另一種可能的解決方案是使用??

var result = collection.GroupBy(item => new { item.PNO ?? string.Empty, item.GCode ?? string.Empty}) 
      .Select(grouping => new 
      { 
       PNO = grouping.Key.PNO, 
       GCode = grouping.Key.GCode,//when this GCode is null, getting error 
       Options = grouping.Select(item => new { Color = item.Color, Size = item.Size, Amount = item.Amount }).ToList() 
      }).ToList(); 
+0

@Mukil - 這有助於你解決它嗎? –

相關問題