通过查询创建Linq组

时间:2014-04-25 22:12:49

标签: c# linq

            //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。我从表格转储中选择的两列是GA_ITEMREM_QTY。我的try1工作正常,我看到两列的内容都打印出来了。我的try1尚未复制Access SQL查询的功能。

我的try2是尝试分组。我的try2 row.Type1.ToString()是可读的,但row.Type2.ToString()在输出窗口中显示为:

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

我真正想做的是在try2中选择GA_ITEMREM_QTY,就像我在try1中做的那样,按照GA_ITEM分组,但是这些字段没有显示在我的g对象的自动完成中。

有没有人知道如何在Linq中执行此操作?

感谢您发帖...

1 个答案:

答案 0 :(得分: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);
    }
}