2011-07-19 134 views
3

我有三個列表,我需要聯合起來LINQ的加入3名單<T>

class Person 
{ 
    public int PersonID{ get; set; } 
    public string FirstName{get; set;} 
    public string LastName {get; set;} 
} 

class Traffic 
{ 
    public DateTime Date{ get; set; } 
    public int PersonID; 
    public int TrafficID; 
} 

class TrafficType 
{ 
    public int TrafficID { get; set; } 
    public string Description { get; set; } 
} 

List<Person> Persons=GetPersons(); 

List<TrafficType> TrafficTypes=GetTrafficTypes(); 

List<Traffic> Traffics=GetTraffics(); 

我需要一個像輸出:

 
PersonID FirstName LastName  Date   Description 

1001  David  ...  2011/07/19 sample description 

回答

4
from person in Persons 
from traffic in traffics 
from trafficType in trafficTypes 
where trafficType.TrafficID = traffic.TrafficID 
where traffic.PersonID = person.PersonID 
select new 
{ 
    PersonID = person.PersonID, 
    .... 
} 
1
var result = Persons.Join(
    Traffics, 
    person => person.PersonID, 
    trafic => trafic.PersonID, 
    (person, trafic) => new 
    { 
     PersonId = person.PersonID, 
     FirstName = person.FirstName, 
     LastName = person.LastName, 
     Date = trafic.Date, 
     TraficId = trafic.TrafficID 
    }).Join(
     TrafficTypes, 
     a => a.TraficId, 
     traficType => traficType.TrafficID, 
     (a, traficType) => new 
     { 
      PersonId = a.PersonId, 
      FirstName = a.FirstName, 
      LastName = a.LastName, 
      Date = a.Date, 
      Description = traficType.Description 
     }); 
1

下面是使用LINQ查詢表達式代碼的完整代碼示例,應該得到正是你要尋找的:

using System; 
using System.Collections.Generic; 
using System.Linq; 

class Person 
{ 
    public int Id { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
} 

class Traffic 
{ 
    public DateTime Date { get; set; } 
    public int PersonId { get; set; } 
    public int TrafficId { get; set; } 
} 

class TrafficType 
{ 
    public int Id { get; set; } 
    public string Description { get; set; } 
} 

class Program 
{ 
    static void Main(string[] args) 
    { 
     var persons = new List<Person>() 
     { 
      new Person() 
      { 
       Id = 1001, 
       FirstName = "David", 
       LastName = "Jones", 
      }, 
     }; 
     var trafficTypes = new List<TrafficType>() 
     { 
      new TrafficType() 
      { 
       Id = 456, 
       Description = "sample description", 
      }, 
     }; 
     var traffics = new List<Traffic>() 
     { 
      new Traffic() 
      { 
       PersonId = 1001, 
       TrafficId = 456, 
       Date = DateTime.Now, 
      }, 
     }; 

     var joinedData = from p in persons 
         from t in traffics 
         from tt in trafficTypes 
         where p.Id == t.PersonId 
          && tt.Id == t.TrafficId 
         select new 
         { 
          PersonId = p.Id, 
          FirstName = p.FirstName, 
          LastName = p.LastName, 
          // Remove time component, if present 
          Date = t.Date.Date, 
          Description = tt.Description, 
         }; 

     foreach (var item in joinedData) 
     { 
      Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}" 
       , item.PersonId 
       , item.FirstName 
       , item.LastName 
       , item.Date.ToShortDateString() // Don't print the time 
       , item.Description 
       ); 
     } 
    } 
} 

程序輸出是:

1001 David Jones 7/19/2011  sample description 
+0

另外請注意,我' D'是一個縮寫,而不是縮寫,所以應該寫成:'Id'(camel case)。 –