2011-07-19 89 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)。 –