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

本文关键字:列表 两个 返回 记录 合并 linq 连接 | 更新日期: 2023-09-27 17:57:27

当前返回类型为:

"System.Collections.GenericList<{a:ConsoleApplication3.Person,b:ConsoleApplication 3.Person>

与SQL联接返回的数据一样,获得具有所有数据的List<Person>的最佳方法是什么?我想在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();

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

首先您应该创建一个新的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,国家=美国,大陆=北美

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

看起来您想要创建新对象

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中的值。

我真的不明白你是如何选择国家是否在第二个国家的第一个列表中的。但你可以这样做:

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上都有多级条件。

您想要一个Tuple:

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();

如果列表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();