在linq中连接两个列表并返回合并记录

时间:2016-07-18 19:21:26

标签: c# linq

目前,返回类型为:

  

“System.Collections.GenericList< {A:ConsoleApplication3.Person,B:ConsoleApplication3.Person>

获取List<Person>所有数据的最佳方法是什么,就像SQL连接会返回一样? 我想在List<Person>中只获得两行:

{Name:Jon, Address=loc1,Country=US,Continent=NA} 

{Name:Ryan, Address=loc2,Country=Germany,Continent=Europe} 
Person person1 = new Person();
person1.Name = "Jon";
person1.Address = "loc1";

Person person2 = new Person();
person2.Name = "Ryan";
person2.Address = "loc2";

Person person3 = new Person();
person3.Name = "Jon";
person3.Country = "US";
person3.Continent = "NA";

Person person4 = new Person();
person4.Name="Ryan";
person4.Country = "Germany";
person4.Continent = "Europe";

list1.Add(person1);
list1.Add(person2);

list2.Add(person3);
list2.Add(person4);

var  result = (from a in list1 
               join b in list2 on a.Name equals b.Name 
               select new {a,b}).ToList();

5 个答案:

答案 0 :(得分:1)

看起来您想要创建新对象

var results = (from a in list1 
               join b in list2 on a.Name equals b.Name 
               select new Person
               { 
                   Name = a.Name,  
                   Address = a.Address,  
                   Country = b.Country,  
                   Continent = b.Continent
               }).ToList();

但是,如果您不知道哪个列表具有值,则可以执行以下操作

var results = (from a in list1 
               join b in list2 on a.Name equals b.Name 
               select new Person
               { 
                   Name = a.Name,  
                   Address = a.Address ?? b.Address,  
                   Country = a.Country ?? b.Country,  
                   Continent = a.Continent ?? b.Continent
               }).ToList();

这将取list1的值,除非它们是null,如果它们是list2,则会取Job1111的值。

答案 1 :(得分:1)

我真的不知道你是如何选择Country是否在第二个列表的第一个列表中。但你可以这样做:

var  result = (from a in list1 
               join b in list2 on a.Name equals b.Name 
               select new Person()
               {
                  Name = a.Name,
                  Address = a.Address
                  Country = b.Country ?? a.Country  
                  Continent = b.Continent ?? a.Continent 
               }).ToList();

您可以根据需要使用条件,甚至可以混合它们以在a和b上具有多级条件。

答案 2 :(得分:1)

首先您应该创建一个新的Person。所以你的查询应该是这样的:

var resulttt = (from a in list1
                join b in list2 on a.Name equals b.Name
                select new Person
                {
                    Name = a.Name,
                    Address = a.Address ?? b.Address,
                    Country = a.Country ?? b.Country,
                    Continent = a.Continent ?? b.Continent
                }).ToList();

其次以正确的格式显示结果,并且您需要覆盖ToString方法,如下所示:

public class Person
{
    public string Name { get; set; }
    public string Address { get; set; }
    public string Country { get; set; }
    public string Continent { get; set; }

    public override string ToString()
    {
        return String.Format("Name = {0}, Address = {1}, Country = {2}, Continent = {3}", Name,Address,Country,Continent);
    }
}

最后通过迭代结果,您将获得所需的结果。像这样:

foreach (var item in result)
{
    Console.WriteLine(item);                
}

输出:

  

姓名:Jon,地址= loc1,国家/地区=美国,大陆= NA

     

姓名:Ryan,地址= loc2,国家=德国,大陆=欧洲

答案 3 :(得分:0)

你想要一个元组:

List<Tuple<Person, Person>> result = (
from a in list1
join b in list2 on a.Name equals b.Name
select Tuple.Create(a, b)
).ToList();

答案 4 :(得分:0)

如果列表1总是有地址,列表2总是有国家和大陆,你的linq语句看起来像

var  result = (from a in list1 join b in list2 on a.Name equals b.Name select new Person { Name = a.Name, Address = a.Address, Country = b.Country, Continent = b.Continent }).ToList();