光脚丫学LINQ(008):将多个输入联接到一个输出序列

视频演示:http://u.115.com/file/f2f372f454

 

可以使用 LINQ 查询来创建包含多个输入序列的元素的输出序列。下面的示例演示如何组合两个内存中的数据结构,但组合来自 XML 或 SQL 或数据集源的数据时可应用相同的原则。假定下面两种类类型:
public class Student { public string FirstName { get; set; } public string LastName { get; set; } public int ID { get; set; } public string Street { get; set; } public string City { get; set; } public List<int> Scores; } public class Teacher { public string FirstName { get; set; } public string LastName { get; set; } public int ID { get; set; } public string City { get; set; } }

 

下面的示例演示该查询:
static void Main(string[] args) { // Create the first data source. List<Student> Students = new List<Student>() { new Student {FirstName="Svetlana", LastName="Omelchenko", ID=111, Street="123 Main Street", City="Seattle", Scores= new List<int> {97, 92, 81, 60}}, new Student {FirstName="Claire", LastName="O’Donnell", ID=112, Street="124 Main Street", City="Redmond", Scores= new List<int> {75, 84, 91, 39}}, new Student {FirstName="Sven", LastName="Mortensen", ID=113, Street="125 Main Street", City="Lake City", Scores= new List<int> {88, 94, 65, 91}}, }; // Create the second data source. List<Teacher> Teachers = new List<Teacher>() { new Teacher {FirstName="Ann", LastName="Beebe", ID=945, City = "Seattle"}, new Teacher {FirstName="Alex", LastName="Robinson", ID=956, City = "Redmond"}, new Teacher {FirstName="Michiyo", LastName="Sato", ID=972, City = "Tacoma"} }; // Create the query. var PeopleInSeattle = (from student in Students where student.City == "Seattle" select student.LastName) .Concat (from teacher in Teachers where teacher.City == "Seattle" select teacher.LastName); Console.WriteLine("The following Students and Teachers live in Seattle:"); // Execute the query. foreach (var Person in PeopleInSeattle) { Console.WriteLine(Person); } Console.WriteLine("Press any key to exit."); Console.ReadKey(); }

你可能感兴趣的:(光脚丫学LINQ(008):将多个输入联接到一个输出序列)