IList<Student> studentList = new List<Student>() { new Student() { StudentID = 1, StudentName = "John" }, new Student() { StudentID = 2, StudentName = "Moin" }, new Student() { StudentID = 3, StudentName = "Bill" }, new Student() { StudentID = 4, StudentName = "Ram" }, new Student() { StudentID = 5, StudentName = "Ron" } }; var selectResult = from s in studentList select s.StudentName;
select可以定义结果数据类型,可以返回用户自定义的类型或者匿名类型
IList<Student> studentList = new List<Student>() { new Student() { StudentID = 1, StudentName = "John", Age = 13 } , new Student() { StudentID = 2, StudentName = "Moin", Age = 21 } , new Student() { StudentID = 3, StudentName = "Bill", Age = 18 } , new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 } , new Student() { StudentID = 5, StudentName = "Ron" , Age = 15 } }; // returns collection of anonymous objects with Name and Age property var selectResult = from s in studentList select new { Name = "Mr. " + s.StudentName, Age = s.Age }; // iterate selectResult foreach (var item in selectResult) Console.WriteLine("Student Name: {0}, Age: {1}", item.Name, item.Age);
方法查询
IList<Student> studentList = new List<Student>() { new Student() { StudentID = 1, StudentName = "John", Age = 18 } , new Student() { StudentID = 2, StudentName = "Moin", Age = 21 } , new Student() { StudentID = 3, StudentName = "Bill", Age = 18 } , new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 } , new Student() { StudentID = 5, StudentName = "Ron" , Age = 21 } }; var selectResult = studentList.Select(s => new { Name = s.StudentName , Age = s.Age });
SelectMany
class Student { public int Score { get; set; } public Student(int score) { this.Score = score; } } class Teacher { public string Name { get; set; } public List<Student> Students; public Teacher(string order,List<Student> students) { this.Name = order; this.Students = students; } }
List<Teacher> teachers = new List<Teacher> { new Teacher("a",new List<Student>{ new Student(100),new Student(90),new Student(30) }), new Teacher("b",new List<Student>{ new Student(100),new Student(90),new Student(60) }), new Teacher("c",new List<Student>{ new Student(100),new Student(90),new Student(40) }), new Teacher("d",new List<Student>{ new Student(100),new Student(90),new Student(60) }), new Teacher("e",new List<Student>{ new Student(100),new Student(90),new Student(50) }), new Teacher("f",new List<Student>{ new Student(100),new Student(90),new Student(60) }), new Teacher("g",new List<Student>{ new Student(100),new Student(90),new Student(60) }) };
这里有7个老师,每个人有3个学生,总共21一个学生里 查询3个倒霉蛋没考及格
var list1 = from t in teachers from s in t.Students where s.Score < 60 select s;
var list2 = teachers.SelectMany(t => t.Students).Where(s => s.Score < 60);
public static IEnumerable<TResult> SelectMany<TSource, TResult>( this IEnumerable<TSource> source, Func<TSource, IEnumerable<TResult>> selector ) public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult>( this IEnumerable<TSource> source, Func<TSource, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector )
选出了门下有不及格学生的倒霉蛋老师+门生的分数
var list3 = teachers.SelectMany( t => t.Students, (t, s) => new { t.Name, s.Score }) .Where(n => n.Score < 60);