将列表分成较小的列表C#

时间:2017-04-23 05:20:21

标签: c#

我正在使用此代码生成一个列表:

        var Data2 = Data.GroupBy(i => new { i.City, i.County, i.Type })
                                    .Select(group => new { Name = group.Key, Number = group.Count() })
                                    .OrderBy(x => x.Name).ThenByDescending(x => x.Number).ToList();

此列表包含约5100条记录。我想生成具有独特城市和县的不同列表。许多列表或数组可以为我工作。你能给我一些想法吗?感谢。

编辑:

例如,此查询返回这样的列表:

    { Name = {{ City = New York City, County  = Bronx, Type = Type A }}, Count = 4 }

    { Name = {{ City = New York City, County  = Bronx, Type = Type B }}, Count = 8 }

    { Name = {{ City = New York City, County  = Bronx, Type = Type C }}, Count = 24 }

    { Name = {{ City = New York City, County  = Manhattan, Type = Type B }}, Count = 43 }

    { Name = {{ City = New York City, County  = Manhattan, Type = Type C }}, Count = 58 }

    { Name = {{ City = Seattle, County  = King County, Type = Type D }}, Count = 43 }

    { Name = {{ City = Seattle, County  = King County, Type = Type A }}, Count = 67 }

    { Name = {{ City = Seattle, County  =    Snohomish County, Type = Type C }}, Count = 67 }

我想将此列表设为以下几个列表:

清单1:

    { Name = {{ City = New York City, County  = Bronx, Type = Type A }}, Count = 4 }

    { Name = {{ City = New York City, County  = Bronx, Type = Type B }}, Count = 8 }

    { Name = {{ City = New York City, County  = Bronx, Type = Type C }}, Count = 24 }

清单2:

    { Name = {{ City = New York City, County  = Manhattan, Type = Type B }}, Count = 43 }

    { Name = {{ City = New York City, County  = Manhattan, Type = Type C }}, Count = 58 }

清单3:

    { Name = {{ City = Seattle, County  = King County, Type = Type D }}, Count = 43 }

    { Name = {{ City = Seattle, County  = King County, Type = Type A }}, Count = 67 }

清单4:

    { Name = {{ City = Seattle, County  =  Snohomish County, Type = Type C }}, Count = 67 }

1 个答案:

答案 0 :(得分:1)

应该这样做:
它是一个小组内的一个小组

var Data2 =
    Data.GroupBy(i => new {i.City, i.County, i.Type})
        .Select(group => new {Name = group.Key, Count = group.Count()})
        .OrderBy(x => x.Name)
        .ThenByDescending(x => x.Count)
        .GroupBy(g => new {g.Name.City, g.Name.County})
        .Select(g => g.Select(g2 => 
            new {Name = new {g.Key.City, g.Key.County, g2.Name.Type}, g2.Count}));