In LINQ where clauses are used for filtering the collection or SQL data. In another word where clauses help to filter the condition-based data for collection or SQL server.
Syntax
datatype variable = from object in collections where condition, where condition select object;
using System; using System.Collections.Generic; using System.Linq;
public class Program { public static void Main() { List<Student> students = new List<Student>() { new Student() { StudentId = 1, Name = 'Asu', Marks = 500 }, new Student() { StudentId = 2, Name = 'Shyam', Marks = 300 }, new Student() { StudentId = 3, Name = 'Shriyam', Marks = 400 }, new Student() { StudentId = 4, Name = 'Sunny', Marks = 550 }, new Student() { StudentId = 5, Name = 'Ram', Marks = 600 }, new Student() { StudentId = 6, Name = 'Krishna', Marks = 700 }, new Student() { StudentId = 7, Name = 'Anupam', Marks = 550 } };
// select projection using first sysntax
var result1= from student in students where student.Marks > 300 && student.Marks < 700 select student ;
Console.WriteLine('Using First Syntax :\n'); result1.ToList().ForEach(item => Console.WriteLine('StudentName is {0}, ID is {1}, and Marks is {2}', item.Name, item.StudentId, item.Marks) );
var result2 = from student in students where student.Marks > 300 where student.Marks < 700 select new {Name =student.Name,ID = student.StudentId,Marks = student.Marks };
Console.WriteLine('\nUsing Second Syntax :\n'); result2.ToList().ForEach(item => Console.WriteLine('StudentName is {0}, ID is {1}, and Marks is {2}', item.Name, item.ID, item.Marks) );
var result3 = students.Where( student => student.StudentId < 5);
Console.WriteLine('\nUsing where method Syntax :\n'); result3.ToList().ForEach(item => Console.WriteLine('StudentName is {0}, ID is {1}, and Marks is {2}', item.Name, item.StudentId, item.Marks) );
Console.ReadLine(); } }
class Student { public int StudentId { get; set; } public string Name { get; set; } public int Marks { get; set; } }
Output
Using First Syntax :
StudentName is Asu, ID is 1, and Marks is 500
StudentName is Shriyam, ID is 3, and Marks is 400
StudentName is Sunny, ID is 4, and Marks is 550
StudentName is Ram, ID is 5, and Marks is 600
StudentName is Anupam, ID is 7, and Marks is 550
Using Second Syntax :
StudentName is Asu, ID is 1, and Marks is 500
StudentName is Shriyam, ID is 3, and Marks is 400
StudentName is Sunny, ID is 4, and Marks is 550
StudentName is Ram, ID is 5, and Marks is 600
StudentName is Anupam, ID is 7, and Marks is 550
Using where method Syntax :
StudentName is Asu, ID is 1, and Marks is 500
StudentName is Shyam, ID is 2, and Marks is 300
StudentName is Shriyam, ID is 3, and Marks is 400
StudentName is Sunny, ID is 4, and Marks is 550
In LINQ where clauses are used for filtering the collection or SQL data. In another word where clauses help to filter the condition-based data for collection or SQL server.
Syntax
datatype variable = from object in collections where condition, where condition select object;
using System; using System.Collections.Generic; using System.Linq; public class Program { public static void Main() { List<Student> students = new List<Student>() { new Student() { StudentId = 1, Name = 'Asu', Marks = 500 }, new Student() { StudentId = 2, Name = 'Shyam', Marks = 300 }, new Student() { StudentId = 3, Name = 'Shriyam', Marks = 400 }, new Student() { StudentId = 4, Name = 'Sunny', Marks = 550 }, new Student() { StudentId = 5, Name = 'Ram', Marks = 600 }, new Student() { StudentId = 6, Name = 'Krishna', Marks = 700 }, new Student() { StudentId = 7, Name = 'Anupam', Marks = 550 } };
// select projection using first sysntax var result1= from student in students where student.Marks > 300 && student.Marks < 700 select student ; Console.WriteLine('Using First Syntax :\n'); result1.ToList().ForEach(item => Console.WriteLine('StudentName is {0}, ID is {1}, and Marks is {2}', item.Name, item.StudentId, item.Marks) );
var result2 = from student in students where student.Marks > 300 where student.Marks < 700 select new {Name =student.Name,ID = student.StudentId,Marks = student.Marks };
Console.WriteLine('\nUsing Second Syntax :\n'); result2.ToList().ForEach(item => Console.WriteLine('StudentName is {0}, ID is {1}, and Marks is {2}', item.Name, item.ID, item.Marks) );
var result3 = students.Where( student => student.StudentId < 5); Console.WriteLine('\nUsing where method Syntax :\n'); result3.ToList().ForEach(item => Console.WriteLine('StudentName is {0}, ID is {1}, and Marks is {2}', item.Name, item.StudentId, item.Marks) );
Console.ReadLine(); } }
class Student { public int StudentId { get; set; } public string Name { get; set; } public int Marks { get; set; } }
Output
Using First Syntax :
StudentName is Asu, ID is 1, and Marks is 500
StudentName is Shriyam, ID is 3, and Marks is 400
StudentName is Sunny, ID is 4, and Marks is 550
StudentName is Ram, ID is 5, and Marks is 600
StudentName is Anupam, ID is 7, and Marks is 550
Using Second Syntax :
StudentName is Asu, ID is 1, and Marks is 500
StudentName is Shriyam, ID is 3, and Marks is 400
StudentName is Sunny, ID is 4, and Marks is 550
StudentName is Ram, ID is 5, and Marks is 600
StudentName is Anupam, ID is 7, and Marks is 550
Using where method Syntax :
StudentName is Asu, ID is 1, and Marks is 500
StudentName is Shyam, ID is 2, and Marks is 300
StudentName is Shriyam, ID is 3, and Marks is 400
StudentName is Sunny, ID is 4, and Marks is 550
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In LINQ where clauses are used for filtering the collection or SQL data. In another word where clauses help to filter the condition-based data for collection or SQL server.
Syntax
Example
Output
In LINQ where clauses are used for filtering the collection or SQL data. In another word where clauses help to filter the condition-based data for collection or SQL server.
Syntax
Example
Output