2013-05-27 56 views
1

你好我的GroupBy查詢出了什麼問題?我的GroupBy查詢出了什麼問題

我有以下類:

public class AssembledPartsDTO 
{ 
    public int PID { get; set; } 
    public McPosition Posiotion { get; set; } 
    public string Partnumber { get; set; } 
    public string ReelID { get; set; } 
    public int BlockId { get; set; } 
    public List<string> References { get; set; } 
} 

我試圖執行以下查詢:

assembledPcb.AssembledParts.GroupBy(entry => new 
         { 
          entry.PID, 
          entry.Posiotion.Station, 
          entry.Posiotion.Slot, 
          entry.Posiotion.Subslot, 
          entry.Partnumber, 
          entry.ReelID, 
          entry.BlockId 
         }). 
         Select((key , val)=> new AssembledPartsDTO 
          { 
           BlockId = key.Key.BlockId, 
           PID = key.Key.PID, 
           Partnumber = key.Key.Partnumber, 
           ReelID = key.Key.ReelID, 
           Posiotion = new McPosition(key.Key.Station, key.Key.Slot, key.Key.Subslot), 
           References = val <-- ???? 
          }) 

val,我有沒有int型的,而不是分組的VAL我可以在那裏執行val.SelectMany(v => v).ToList();任何想法我的代碼中有什麼問題?

回答

5

Enumerable.Select的第二個參數是序列中項目的索引。所以在這種情況下,它是該組的(基於零)號碼。你只是想選擇組,你不需要它的指數:

var result = assembledPcb.AssembledParts.GroupBy(entry => new 
{ 
    entry.PID, 
    entry.Posiotion.Station, 
    entry.Posiotion.Slot, 
    entry.Posiotion.Subslot, 
    entry.Partnumber, 
    entry.ReelID, 
    entry.BlockId 
}) 
.Select(g => new AssembledPartsDTO 
{ 
    BlockId = g.Key.BlockId, 
    PID = g.Key.PID, 
    Partnumber = g.Key.Partnumber, 
    ReelID = g.Key.ReelID, 
    Posiotion = new McPosition(g.Key.Station, g.Key.Slot, g.Key.Subslot), 
    References = g.SelectMany(entry => entry.References) 
        .Distinct() 
        .ToList() 
}); 

(假設你想不同的參考列表)

側面說明:您的財產而有一個錯字名稱:Posiotion