C# 使用公用键连接和分组2列表

C# 使用公用键连接和分组2列表,c#,linq,C#,Linq,我有两个不同类的列表,其中State(在Pet中)和Region(在Person中)可以包含相同的字符串值。我试图得到如下输出: 州/地区1: 美国的宠物1 区域1的人们 应该为每个键值显示结果,即使其中一个列表中没有。我编写的代码很有效,但看起来很笨重。我想知道是否有其他的方法来完成它 Person magnus = new Person { FirstName = "Magnus", LastName = "Hedlund", Region="Texas" };

我有两个不同类的列表,其中State(在Pet中)和Region(在Person中)可以包含相同的字符串值。我试图得到如下输出:

州/地区1:

  • 美国的宠物1

  • 区域1的人们

应该为每个键值显示结果,即使其中一个列表中没有。我编写的代码很有效,但看起来很笨重。我想知道是否有其他的方法来完成它

        Person magnus = new Person { FirstName = "Magnus", LastName = "Hedlund", Region="Texas" };
        Person terry = new Person { FirstName = "Terry", LastName = "Adams", Region="NY" };
        Person charlotte = new Person { FirstName = "Charlotte", LastName = "Weiss", Region="Texas" };
        Person arlene = new Person { FirstName = "Arlene", LastName = "Huff",Region="Corolado" };
        Person rui = new Person { FirstName = "Rui", LastName = "Raposo",Region="Texas" };
        Person phyllis = new Person { FirstName = "Phyllis", LastName = "Harris", Region="California" };

        Pet barley = new Pet { Name = "Barley", Owner = terry, State="NY" };
        Pet boots = new Pet { Name = "Boots", Owner = terry, State="New Jersey" };
        Pet whiskers = new Pet { Name = "Whiskers", Owner = charlotte, State="Texas" };
        Pet daisy = new Pet { Name = "Daisy", Owner = magnus, State="NY" };

        List<Person> people = new List<Person> { magnus, terry, charlotte,arlene, rui,phyllis };
        List<Pet> pets = new List<Pet> { barley, boots, whiskers, daisy };

        //join pets with associated people on Region\State
        var resultQuery =( from pet in pets
                       group pet by pet.State into petGroup
                       join person in people on petGroup.Key equals person.Region into persGroup
                           select new Container { Key = petGroup.Key, Pets = petGroup, People = persGroup }
                       ).ToList();

        var persQuery = from person in people
                        group person by person.Region;
        //add people groups that have no equivalent in pet groups to the result
        foreach (IGrouping<string, Person> gr in persQuery)
        {
            if (resultQuery.All(_container => !_container.Key.Contains(gr.Key)))
                resultQuery.Add(new Container { Key = gr.Key, Pets = new List<Pet>(), People = gr });//have to use Container here
        }

        //show results
        foreach (var _container in resultQuery)
        {
            Console.WriteLine("Key(Region): {0}", _container.Key);
            foreach (Pet pet in _container.Pets) Console.WriteLine(pet.Name);
            foreach (Person per in _container.People)
                Console.WriteLine(per.FirstName);
        }
Person magnus=新人{FirstName=“magnus”,LastName=“Hedlund”,Region=“Texas”};
Person terry=新人{FirstName=“terry”,LastName=“Adams”,Region=“NY”};
Person charlotte=新人{FirstName=“charlotte”,LastName=“Weiss”,Region=“Texas”};
Person-arlene=新人{FirstName=“arlene”,LastName=“Huff”,Region=“Corolado”};
Person rui=新人{FirstName=“rui”,LastName=“Raposo”,Region=“Texas”};
Person phyllis=新人{FirstName=“phyllis”,LastName=“Harris”,Region=“California”};
宠物大麦=新宠物{Name=“大麦”,所有者=特里,State=“NY”};
宠物靴=新宠物{Name=“boots”,主人=特里,State=“newjersey”};
宠物胡须=新宠物{Name=“胡须”,所有者=夏洛特州=“德克萨斯州”};
宠物雏菊=新宠物{Name=“daisy”,所有者=马格纳斯,State=“NY”};
名单人物=新名单{magnus、terry、charlotte、arlene、rui、phyllis};
宠物名单=新名单{大麦、靴子、胡须、雏菊};
//在Region\State上与相关人员加入宠物
var resultQuery=(来自宠物中的宠物)
按宠物分组。将宠物分组
将人加入petGroup上的人中。键等于人。区域加入persGroup
选择新容器{Key=petGroup.Key,Pets=petGroup,People=pergroup}
).ToList();
var persQuery=从人到人
一人一组。区域;
//向结果中添加在宠物组中没有等效项的人员组
foreach(在persQuery中对gr进行分组)
{
if(resultQuery.All(\u container=>!\u container.Key.Contains(gr.Key)))
resultQuery.Add(新容器{Key=gr.Key,Pets=newlist(),People=gr});//必须在这里使用容器
}
//显示结果
foreach(resultQuery中的var\u容器)
{
WriteLine(“Key(Region):{0}”,_container.Key);
foreach(Pet-in _-container.Pets)控制台.WriteLine(Pet.Name);
foreach(每个集装箱中的人)
Console.WriteLine(根据FirstName);
}


此外,我还必须添加类容器,而不是使用匿名类型来执行。添加方法我使用了union和group by,可能是更好的方法。除了这个解决方案,我现在什么也没想到。试试这个

Person magnus = new Person { FirstName = "Magnus", LastName = "Hedlund", Region = "Texas" };
        Person terry = new Person { FirstName = "Terry", LastName = "Adams", Region = "NY" };
        Person charlotte = new Person { FirstName = "Charlotte", LastName = "Weiss", Region = "Texas" };
        Person arlene = new Person { FirstName = "Arlene", LastName = "Huff", Region = "Corolado" };
        Person rui = new Person { FirstName = "Rui", LastName = "Raposo", Region = "Texas" };
        Person phyllis = new Person { FirstName = "Phyllis", LastName = "Harris", Region = "California" };

        Pet barley = new Pet { Name = "Barley", Owner = terry, State = "NY" };
        Pet boots = new Pet { Name = "Boots", Owner = terry, State = "New Jersey" };
        Pet whiskers = new Pet { Name = "Whiskers", Owner = charlotte, State = "Texas" };
        Pet daisy = new Pet { Name = "Daisy", Owner = magnus, State = "NY" };

        List<Person> people = new List<Person> { magnus, terry, charlotte, arlene, rui, phyllis };
        List<Pet> pets = new List<Pet> { barley, boots, whiskers, daisy };


        var petsGroupQuery = (from pet in pets
                              group pet by pet.State into petGroup
                              select new Container { Key = petGroup.Key, Pets = petGroup.Select(x => x).ToList(), People = new  List<Person> ()}).Union
                              (from pep in people
                               group pep by pep.Region into pepGroup
                               select new Container { Key = pepGroup.Key, Pets = new List<Pet>(), People = pepGroup.Select(x => x).ToList() }).GroupBy
                               (x => x.Key)
                               .Select(t => new Container {Key = t.Key, People = t.SelectMany(x => x.People), Pets = t.SelectMany(x => x.Pets) });
        //// printing
        petsGroupQuery.ToList().ForEach(x =>
            {
                Trace.WriteLine(x.Key);
                Trace.WriteLine(string.Join(",", x.People.Select(t => t.FirstName)));
                Trace.WriteLine(string.Join(",", x.Pets.Select(t => t.Name)));
            });
Person magnus=新人{FirstName=“magnus”,LastName=“Hedlund”,Region=“Texas”};
Person terry=新人{FirstName=“terry”,LastName=“Adams”,Region=“NY”};
Person charlotte=新人{FirstName=“charlotte”,LastName=“Weiss”,Region=“Texas”};
Person-arlene=新人{FirstName=“arlene”,LastName=“Huff”,Region=“Corolado”};
Person rui=新人{FirstName=“rui”,LastName=“Raposo”,Region=“Texas”};
Person phyllis=新人{FirstName=“phyllis”,LastName=“Harris”,Region=“California”};
宠物大麦=新宠物{Name=“大麦”,所有者=特里,State=“NY”};
宠物靴=新宠物{Name=“boots”,主人=特里,State=“newjersey”};
宠物胡须=新宠物{Name=“胡须”,所有者=夏洛特州=“德克萨斯州”};
宠物雏菊=新宠物{Name=“daisy”,所有者=马格纳斯,State=“NY”};
名单人物=新名单{magnus、terry、charlotte、arlene、rui、phyllis};
宠物名单=新名单{大麦、靴子、胡须、雏菊};
var petsGroupQuery=(来自pets中的pet)
按宠物分组。将宠物分组
选择新容器{Key=petGroup.Key,Pets=petGroup.select(x=>x).ToList(),People=newlist()}).Union
(来自《人物》中的政治公众人物)
按政治公众人物区域将政治公众人物分组为政治公众人物组
选择新容器{Key=pepGroup.Key,Pets=new List(),People=pepGroup.select(x=>x).ToList()}).GroupBy
(x=>x.Key)
.Select(t=>newcontainer{Key=t.Key,People=t.SelectMany(x=>x.People),Pets=t.SelectMany(x=>x.Pets)};
////印刷品
petsGroupQuery.ToList().ForEach(x=>
{
Trace.WriteLine(x.Key);
Trace.WriteLine(string.Join(“,”,x.People.Select(t=>t.FirstName));
Trace.WriteLine(string.Join(“,”,x.Pets.Select(t=>t.Name));
});

。GroupBy可能就是答案