Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将集合拆分为多个集合_C#_Linq_Lambda - Fatal编程技术网

C# 将集合拆分为多个集合

C# 将集合拆分为多个集合,c#,linq,lambda,C#,Linq,Lambda,我想在单个列表的基础上创建列表的新列表。 我有一张这样的清单 public class OfficeLocator { public String id{ get; set; } public string Geography{ get; set; } public string Country{ get; set; } public string State{ get; set; } public string OfficeName{ get; set;

我想在单个列表的基础上创建列表的新列表。 我有一张这样的清单

public class OfficeLocator
{
    public String id{ get; set; }
    public string Geography{ get; set; }
    public string Country{ get; set; }
    public string State{ get; set; }
    public string OfficeName{ get; set; }
}
我正在准备一个树状结构的列表

GeographyName="Asia",
{ 
   Country = "China",
   {
      State = "Hunan",
        {
            {
                OfficeId = "1",
                OfficeName = "Office 1"
            },
            {
                OfficeId = "2",
                OfficeName = "Office 2"
            }
        },
      State = "Hubei"
        {
            {
                OfficeId = "3",
                OfficeName = "Office 3"
            }
        }
    },
    Country = "India",
    {
      State = "Maharashtra",
        {
            {
                OfficeId = "4",
                OfficeName = "Office 4"
            },
            {
                OfficeId = "5",
                OfficeName = "Office 5"
            }
        },
      State = "Punjab"
        {
            {
                OfficeId = "6",
                OfficeName = "Office 6"
            }
        }
    },
},
GeographyName="Europe",
{ 
   Country = "UK",
   {
      State = "York",
        {
            {
                OfficeId = "7",
                OfficeName = "Office 7"
            },
            {
                OfficeId = "8",
                OfficeName = "Office 8"
            }
        }     
    }
}
我试着在地理和国家方面使用一些分组。 但我没有得到所需的输出。
我可以使用循环逻辑来获得结果,但我想避免它,并尝试使用linq。

类似的东西

var allRegionGroups = allOfficeLocators
    .GroupBy(ol => ol.Geography)
    .Select(gGroup => new
    {
        GeographyName = gGroup.Key,
        Countries = gGroup
            .GroupBy(ol => ol.Country)
            .Select(cGroup => new
            {
                Country = cGroup.Key,
                States = cGroup
                  .GroupBy(ol => ol.State)
                  .Select(sGroup => new
                  {
                      State = sGroup.Key,
                      OfficeList = sGroup
                       .Select(ol => new { OfficeId = ol.id, ol.OfficeName })
                       .ToList()
                  })
                 .ToList()
            })
            .ToList()
    })
    .ToList();
如何访问匿名类型的所有属性:

foreach (var region in allRegionGroups)
{
    string geographyName = region.GeographyName;
    var allCountries = region.Countries;
    foreach (var c in allCountries)
    {
        string country = c.Country;
        var allStates = c.States;
        //  and so on...
    }
}
像这样的

var allRegionGroups = allOfficeLocators
    .GroupBy(ol => ol.Geography)
    .Select(gGroup => new
    {
        GeographyName = gGroup.Key,
        Countries = gGroup
            .GroupBy(ol => ol.Country)
            .Select(cGroup => new
            {
                Country = cGroup.Key,
                States = cGroup
                  .GroupBy(ol => ol.State)
                  .Select(sGroup => new
                  {
                      State = sGroup.Key,
                      OfficeList = sGroup
                       .Select(ol => new { OfficeId = ol.id, ol.OfficeName })
                       .ToList()
                  })
                 .ToList()
            })
            .ToList()
    })
    .ToList();
如何访问匿名类型的所有属性:

foreach (var region in allRegionGroups)
{
    string geographyName = region.GeographyName;
    var allCountries = region.Countries;
    foreach (var c in allCountries)
    {
        string country = c.Country;
        var allStates = c.States;
        //  and so on...
    }
}
,具有一些在这种情况下很方便的过载:

var query = locations.GroupBy(
            x => x.Geography,
            (a, aGroup) => new
            {
                A = a,
                Items = aGroup.GroupBy(
                    x => x.Country,
                    (b, bGroup) => new
                    {
                        B = b,
                        Items = bGroup.GroupBy(
                            x => x.State,
                            (c, cGroup) => new
                            {
                                B = c,
                                Items = cGroup.Select(i => new {i.Id, i.OfficeName})
                            })
                    })
            });
读写起来更清晰一点。
如果需要,Anonimized对象可以由您自己的类替换

,在这种情况下会有一些过载:

var query = locations.GroupBy(
            x => x.Geography,
            (a, aGroup) => new
            {
                A = a,
                Items = aGroup.GroupBy(
                    x => x.Country,
                    (b, bGroup) => new
                    {
                        B = b,
                        Items = bGroup.GroupBy(
                            x => x.State,
                            (c, cGroup) => new
                            {
                                B = c,
                                Items = cGroup.Select(i => new {i.Id, i.OfficeName})
                            })
                    })
            });
读写起来更清晰一点。

如果需要,Anonimized对象可以由您自己的类替换

你可以达到蒂姆提到的同样的结果 但不是使用方法语法, 依赖查询语法以获得更好的可读性

var projection = from location in locations
                 group location by location.Geography into geographies
                 select new
                 {
                    Geography = geographies.Key,
                    Countries = from geography in geographies
                                group geography by geography.Country into countries
                                select new
                                {
                                    Country = countries.Key,
                                    States = from country in countries
                                            group country by country.State into states
                                            select new
                                            {
                                                State = states.Key,
                                                Offices = from state in states
                                                        select new
                                                        {
                                                            Id = state.Id,
                                                            Office = state.Office,
                                                        }
                                             }
                                }
                };
最终结果是这样的


您可以获得与Tim提到的相同的结果 但不是使用方法语法, 依赖查询语法以获得更好的可读性

var projection = from location in locations
                 group location by location.Geography into geographies
                 select new
                 {
                    Geography = geographies.Key,
                    Countries = from geography in geographies
                                group geography by geography.Country into countries
                                select new
                                {
                                    Country = countries.Key,
                                    States = from country in countries
                                            group country by country.State into states
                                            select new
                                            {
                                                State = states.Key,
                                                Offices = from state in states
                                                        select new
                                                        {
                                                            Id = state.Id,
                                                            Office = state.Office,
                                                        }
                                             }
                                }
                };
最终结果是这样的


当你说“列表”时,你的意思是
list
type collection of C#,还是一个简单的文本列表作为JSON?展示你的方法我的意思是list type collection of C#你可以检查并指定你的问题吗。现在,它看起来像是给我指出了正确的方向。当你说“列表”时,你是指
list
type-collection of C#,还是指一个简单的文本列表作为JSON?展示你的方法我指的是list-type-collection of C#,你可以检查并指定你的问题吗。目前看来,它似乎为我指明了正确的方向。感谢您提供的解决方案,但是有没有办法将集合作为列表,这样我就可以直接从中筛选数据,而不是使用foreach循环。它是一个列表,当我试图将其转换为任何类型时,您可以直接查询它。我得到的匿名类型转换错误是type to List。例如:如果我尝试创建它的树结构,并使用类节点{string id;string name;List Nodes}之类的递归逻辑,那么您还没有显示您的类,因此我无法创建它的实例。您必须使用new ClassNane,我使用new{}创建匿名类型。是的,我这样做了,而不是使用new,我有我的类名,但对于最后一个节点,它给出了一个转换列表的例外,无法从匿名转换为解决方案,但是有没有办法将集合作为列表,这样我就可以直接从中筛选数据,而不是使用foreach循环。它是一个列表,您可以直接查询它。当我试图将它转换为任何类型时,我得到了匿名类型转换为列表的类型转换错误。例如:如果我尝试创建它的树结构,并使用类节点{string id;string name;List Nodes}之类的递归逻辑,那么您还没有显示您的类,因此我无法创建它的实例。您必须使用new ClassNane,我使用new{}创建匿名类型。是的,我这样做了,而不是new,我有我的类名,但对于最后一个节点,它给出了一个转换列表的异常,无法从匿名转换。我尝试了与我的一个类似的方法。但没有取得任何成功。你能帮我吗?嗨,我试着用类似的方法来处理我的一个问题。但没有取得任何成功。你能帮我吗。