2016-07-18 129 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的多級條件的條件發揮。