2016-07-18 101 views
0

目前,返回类型是:加盟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(); 

回答

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

输出:

名称:乔恩,地址= LOC1,国家=美国,欧洲大陆= NA

名称:瑞安,地址= LOC2,国家=德国,欧洲大陆=欧洲

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

,除非他们是null,如果他们是这反而需要从价值,它将从list1list2

1

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

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的多级条件的条件发挥。