2014-04-25 103 views
0
  //SELECT table1.GG_ITEM, Sum(table1.REM_QTY) AS SumPerGG_ITEM 
      //FROM table1 
      //WHERE (table1.SUGG_DOCK_DATE Is Not Null) 
      //GROUP BY table1.GG_ITEM 
      //ORDER BY table1.GG_ITEM; 

      var try1 = (from row in db2.Dumps select new { Type1 = row.GA_ITEM, Type2 = row.REM_QTY }); 

      Debug.Print(":::::try1:::::"); 
      foreach (var row in try1) 
      { 
       Debug.Print(row.Type1.ToString()); 
       Debug.Print(row.Type2.ToString()); 
      } 

      var try2 = (from row in db2.Dumps group row by row.GA_ITEM into g select new { Type1 = g.Key, Type2 = g.ToList() }); 

      Debug.Print("::::try2:::::"); 
      foreach (var row in try2) 
      { 
       Debug.Print(row.Type1.ToString()); 
       Debug.Print(row.Type2.ToString()); 
      } 

我正在將Access SQL查詢轉換爲Linq。我從我的表Dumps選擇的兩列是GA_ITEMREM_QTY。我的try1工作得很好,我看到兩欄的內容都打印出來了。我的try1尚未複製Access SQL查詢的功能。我的try2是一個分組嘗試。對於我try2row.Type1.ToString()是可讀然而row.Type2.ToString()是顯示了在輸出窗口:通過查詢創建Linq組

System.Collections.Generic.List`1[garminaspsandbox3.Models.Dump] 

我真正希望做的是try2選擇GA_ITEMREM_QTY就像我在try1和組未通過GA_ITEM但是這些領域沒有出現在我的g對象的自動完成中。

有沒有人知道如何在Linq做到這一點?

感謝您張貼...

回答

1

Type2屬性包含List,而不是一個單一的項目,所以你需要使用另一個循環,遍歷物品組:

foreach (var row in try2) 
{ 
    Debug.Print(row.Type1.ToString()); 
    foreach(var item in row.Type2) 
    { 
     Debug.Print(item.GA_ITEM); 
     Debug.Print(item.REM_QTY); 
    } 
}