光脚丫学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(); }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值