Listview分组

时间:2012-08-27 19:51:32

标签: c# .net linq

我遇到过这个教程

http://mattberseth.com/blog/2008/01/building_a_grouping_grid_with.html

但是我很难在C Sharp中编写LinqDataSource查询。

var o = (from c in x group c by c.Date into cc select new { Group = cc.Key, ItemCount = cc.Count() });

以上情况很好,但如何根据我发布的链接中的LinqDataSource将“It as Items”添加到上述查询中。

2 个答案:

答案 0 :(得分:0)

我在您的查询中添加了代码

  var o = 
    (from c in x 
     group c by c.Date into cc 
     select new { Group = cc.Key, ItemCount = cc.Count() });

    foreach (var grp in o)
    {
        Console.WriteLine("\nCategoryID Key = {0}:", grp.Key);
        foreach (var item in grp)
        {
            Console.WriteLine("\t{0}", item.Items);//Replace with your property
        }
    }

您可以使用yield来枚举

答案 1 :(得分:0)

group子句创建IGroupingIGrouping基本上是带有密钥的IEnumerable,因此访问其项目就像访问IEnumerable项目一样。此声明使用ToList(),但您也可以使用AsEnumerable()ToArray()

var o = from c in x 
        group c by c.Date into cc 
        select new { Group = cc.Key, Items = cc.ToList(), ItemCount = cc.Count() };