using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication5 {
internal class Program {
static void Main(string[] args) {
//创建第一个数据源
//创建学生1号
var Students = new List<Student> {
new Student () {
Age = 18,
City = "广州",
Name ="学生1号",
Scores = new List<int>() {11,22,33 }
},
//创建学生2号
new Student () {
Age = 19,
City = "深圳",
Name ="学生2号",
Scores = new List<int>() {44,55,66 }
},
//创建学生3号
new Student () {
Age = 20,
City = "上饶",
Name ="学生3号",
Scores = new List<int>() {88,99 }
},
};
//创建第二个数据源
//创建老师1号
var Tearchers = new List<Teacher>() { //泛型
new Teacher() {
Age = 18,
City = "深圳",
Name = "老师1号",
},
//创建老师2号
new Teacher() {
Age = 18,
City = "上饶",
Name ="老师2号",
},
//创建老师3号
new Teacher() {
Age = 18,
City = "上饶",
Name ="老师3号",
}
};
//创建查询
var peopleInDreams = (from student in Students
where student.City == "上饶"
select student.Name)
.Concat(from Tearcher in Tearchers
where Tearcher.City == "深圳"
select Tearcher.Name);
//执行查询
foreach (var name in peopleInDreams) {
Console.WriteLine("peopleInDreams:" + name);//打印输出在上饶的学生和在深圳的老师
}
//用foreach循环,in左边是变量名,右边是集合名
foreach (var student in Students )
{
Console.WriteLine(student.City);//输出学生的城市
}
Console.Read();
}
}
}
class Student {
public string Name { get; set; }
public int Age { get; set; }
public string City { get; set; }
public List<int> Scores { get; set; }
}
class Teacher {
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string City { get; set; }
}
c#linq学习
于 2022-01-19 15:58:08 首次发布