7. 关键字

let关键字

  • 不使用let关键字
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 }
};
//需要做2次的小写转化
var lowercaseStudentNames = from s in studentList
                            where s.StudentName.ToLower().StartsWith("r")
                            select s.StudentName.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关键字

var teenAgerStudents = from s in studentList
    where s.age > 12 && s.age < 20
    select s
        into teenStudents
        where teenStudents.StudentName.StartsWith("B")
        select teenStudents;
posted @ 2020-06-10 17:57  maanshancss  阅读(105)  评论(0编辑  收藏  举报