LINQ 学习路程 -- 查询操作 let into关键字

IList<Student> studentList = new List<Student>() { 
    new Student() { StudentID = 1, StudentName = "John", Age = 18 } ,
    new Student() { StudentID = 2, StudentName = "Steve",  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 lowercaseStudentNames = from s in studentList
                            where s.StudentName.ToLower().StartsWith("r")
                            select s.StudentName.ToLower();

 

上面的查询使用了多次的ToLower方法

下面的例子中使用let关键字重新引进一个变量,可以在查询中的任何地方使用,让代码可读性更高

var lowercaseStudentNames = from s in studentList
                            let lowercaseStudentName = s.StudentName.ToLower()
                                where lowercaseStudentName.StartsWith("r")
                                select lowercaseStudentName;

foreach (var name in lowercaseStudentNames)
    Console.WriteLine(name);

 

 

into关键字,可以使你在一个select语句之后继续一个查询

var teenAgerStudents = from s in studentList
    where s.age > 12 && s.age < 20
    select s
        into teenStudents
        where teenStudents.StudentName.StartsWith("B")
        select teenStudents;

 

上面的查询中,into关键字引进新的变量,所以从第一个变量s作用域出来,你可以用新的变量写出另一个查询

 

posted @ 2017-03-25 12:21  蓝平凡  阅读(553)  评论(0编辑  收藏  举报