Thiyagarajan E

Thiyagarajan E

  • 1.1k
  • 866
  • 40.9k

Lambda expression vs LINQ Query Expression in c#

May 8 2019 10:16 AM
What is the difference between Lambda expression and LINQ Query expression in c#?
 
Please refer the below code.
 
  1. // Student collection  
  2. IList<Student> studentList = new List<Student>() {  
  3. new Student() { StudentID = 1, StudentName = "John", Age = 13} ,  
  4. new Student() { StudentID = 2, StudentName = "Moin", Age = 21 } ,  
  5. new Student() { StudentID = 3, StudentName = "Bill", Age = 18 } ,  
  6. new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} ,  
  7. new Student() { StudentID = 5, StudentName = "Ron" , Age = 15 }  
  8. };   
  1. // Method -1 LINQ Query Syntax to find out teenager students  
  2. var teenAgerStudent = from s in studentList  
  3. where s.Age > 12 && s.Age < 20  
  4. select s;  
  5.    
  6.    
  7. // Method -2 Lambda expression to find out teenager students  
  8. var teenAgerStudent1 = studentList.Where(x=>x.Age > 12 && x.Age < 20).Select(x=>x).ToList();  
Which is best method and why ?
Difference between method-1 and method-2?
 

Answers (2)