Hello friends, In this tutorial, you are going to understand what is Where clause Filtering operator in Linq. The Where operator is used to filter a collection based on the given criteria expression and return a new collection.
Where Clause Filtering Operator in Linq:
The Where operator is used to filter a collection based on the given criteria expression and return a new collection. Where Clause is not mandatory for LINQ statement but can be used to limit the number of records. It can also be used for extracting those records which we don’t want to select, update or delete. The where clause can be placed anywhere except it cannot be the first or last clause.
Example:
We are going to understand where clause operator using two types of examples.
- Using where clause in Query Syntax.
- Using Where extension method in Method Syntax.
1- Using where clause in Query Syntax:
Example 1 Select specific record:
I want to get details of Employee with Employee Code “E001”.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Runtime.Serialization.Json; namespace StaticClassDemo { public class Employee { public string EmployeeCode { get; set; } public string EmployeeName { get; set; } public string Gender { get; set; } public string Designation { get; set; } public double Salary { get; set; } } class Program { public static List<Employee> DataSource() { List<Employee> lstEmployee = new List<Employee>(); Employee objEmployee1 = new Employee() { EmployeeCode = "E001", EmployeeName = "Ayush Singh", Gender = "Male", Designation = "Software Engineer", Salary = 30000 }; Employee objEmployee2 = new Employee() { EmployeeCode = "E002", EmployeeName = "Ritwik Nigam", Gender = "Male", Designation = "Software Engineer", Salary = 30000 }; Employee objEmployee3 = new Employee() { EmployeeCode = "E003", EmployeeName = "Nikhil Bakshi", Gender = "Male", Designation = "Team Lead", Salary = 60000 }; Employee objEmployee4 = new Employee() { EmployeeCode = "E004", EmployeeName = "Nidhi Verma", Gender = "Female", Designation = "HR", Salary = 40000 }; Employee objEmployee5 = new Employee() { EmployeeCode = "E005", EmployeeName = "Ayushman Lakhotia", Gender = "Male", Designation = "Project Manager", Salary = 80000 }; Employee objEmployee6 = new Employee() { EmployeeCode = "E006", EmployeeName = "Neha Sharma", Gender = "Female", Designation = "Graphic Designer", Salary = 25000 }; lstEmployee.Add(objEmployee1); lstEmployee.Add(objEmployee2); lstEmployee.Add(objEmployee3); lstEmployee.Add(objEmployee4); lstEmployee.Add(objEmployee5); lstEmployee.Add(objEmployee6); return lstEmployee; } static void Main(string[] args) { var vEmployee=from employee in DataSource() where employee.EmployeeCode=="E001" select employee; foreach (Employee objEmployee in vEmployee) { Console.WriteLine("Employee Name:{0}", objEmployee.EmployeeName); Console.WriteLine("Gender:{0}", objEmployee.Gender); Console.WriteLine("Designation:{0}", objEmployee.Designation); Console.WriteLine("Salary:{0}", objEmployee.Salary); } Console.ReadLine(); } } }
Output:
Employee Name:Ayush Singh Gender:Male Designation:Software Engineer Salary:30000
Example 2 Where clause with single condition:
I want to select Employee having salary greater than 30000 var vEmployee=from employee in DataSource() where employee.Salary>30000 select employee; foreach (Employee objEmployee in vEmployee) { Console.WriteLine("Employee Name:{0}", objEmployee.EmployeeName); Console.WriteLine("Gender:{0}", objEmployee.Gender); Console.WriteLine("Designation:{0}", objEmployee.Designation); Console.WriteLine("Salary:{0}", objEmployee.Salary); Console.WriteLine("------------------------------------------"); }
Output:
Employee Name:Nikhil Bakshi Gender:Male Designation:Team Lead Salary:60000 ------------------------------------------ Employee Name:Nidhi Verma Gender:Female Designation:HR Salary:40000 ------------------------------------------ Employee Name:Ayushman Lakhotia Gender:Male Designation:Project Manager Salary:80000 ------------------------------------------
EXAMPLE 3 WHERE CLAUSE WITH multiple CONDITION:
I want to select Employee having salary greater than 30000 and less than 50000
var vEmployee=from employee in DataSource() where employee.Salary>30000 && employee.Salary<50000 select employee; foreach (Employee objEmployee in vEmployee) { Console.WriteLine("Employee Name:{0}", objEmployee.EmployeeName); Console.WriteLine("Gender:{0}", objEmployee.Gender); Console.WriteLine("Designation:{0}", objEmployee.Designation); Console.WriteLine("Salary:{0}", objEmployee.Salary); Console.WriteLine("------------------------------------------"); }
Output:
Employee Name:Nidhi Verma Gender:Female Designation:HR Salary:40000
Example 4 Condition On String Value:
I want to select Employee where Employee Name Starts With “A”:
var vEmployee = from employee in DataSource() where employee.EmployeeName.StartsWith("A") select employee; foreach (Employee objEmployee in vEmployee) { Console.WriteLine("Employee Name:{0}", objEmployee.EmployeeName); Console.WriteLine("Gender:{0}", objEmployee.Gender); Console.WriteLine("Designation:{0}", objEmployee.Designation); Console.WriteLine("Salary:{0}", objEmployee.Salary); Console.WriteLine("------------------------------------------"); }
Output:
Employee Name:Ayush Singh Gender:Male Designation:Software Engineer Salary:30000 ------------------------------------------ Employee Name:Ayushman Lakhotia Gender:Male Designation:Project Manager Salary:80000 ------------------------------------------
Example 5 Using Func Type Delegate:
I want to select Employees having designation Team Lead or Project Manager. For this I am creating a Func Type Delegate.
Func<Employee, bool> IsTLOrPM = delegate(Employee objEmployee) { return objEmployee.Designation == "Team Lead" || objEmployee.Designation == "Project Manager"; }; var vEmployee = from employee in DataSource() where IsTLOrPM(employee) select employee; foreach (Employee objEmployee in vEmployee) { Console.WriteLine("Employee Name:{0}", objEmployee.EmployeeName); Console.WriteLine("Gender:{0}", objEmployee.Gender); Console.WriteLine("Designation:{0}", objEmployee.Designation); Console.WriteLine("Salary:{0}", objEmployee.Salary); Console.WriteLine("------------------------------------------"); }
Output:
Employee Name:Nikhil Bakshi Gender:Male Designation:Team Lead Salary:60000 ------------------------------------------ Employee Name:Ayushman Lakhotia Gender:Male Designation:Project Manager Salary:80000 ------------------------------------------
Using Where extension method in Method Syntax:
I am going to write query with where extension method in method syntax for the above example
EXAMPLE 1 SELECT SPECIFIC RECORD:
I want to get details of Employee with Employee Code “E001”.
var vEmployee = DataSource().Where(e => e.EmployeeCode == "E001"); foreach (Employee objEmployee in vEmployee) { Console.WriteLine("Employee Name:{0}", objEmployee.EmployeeName); Console.WriteLine("Gender:{0}", objEmployee.Gender); Console.WriteLine("Designation:{0}", objEmployee.Designation); Console.WriteLine("Salary:{0}", objEmployee.Salary); Console.WriteLine("------------------------------------------"); }
EXAMPLE 2 WHERE CLAUSE WITH SINGLE CONDITION:
I want to select Employee having salary greater than 30000
var vEmployee = DataSource().Where(e => e.Salary >30000); foreach (Employee objEmployee in vEmployee) { Console.WriteLine("Employee Name:{0}", objEmployee.EmployeeName); Console.WriteLine("Gender:{0}", objEmployee.Gender); Console.WriteLine("Designation:{0}", objEmployee.Designation); Console.WriteLine("Salary:{0}", objEmployee.Salary); Console.WriteLine("------------------------------------------"); }
EXAMPLE 3 WHERE CLAUSE WITH MULTIPLE CONDITION:
I want to select Employee having a salary greater than 30000 and less than 50000
var vEmployee = DataSource().Where(e => e.Salary >30000 && e.Salary<=50000); foreach (Employee objEmployee in vEmployee) { Console.WriteLine("Employee Name:{0}", objEmployee.EmployeeName); Console.WriteLine("Gender:{0}", objEmployee.Gender); Console.WriteLine("Designation:{0}", objEmployee.Designation); Console.WriteLine("Salary:{0}", objEmployee.Salary); Console.WriteLine("------------------------------------------"); }
EXAMPLE 4 CONDITION ON STRING VALUE:
I want to select Employee where Employee Name Starts With “A”:
var vEmployee = DataSource().Where(e => e.EmployeeName.StartsWith("A")); foreach (Employee objEmployee in vEmployee) { Console.WriteLine("Employee Name:{0}", objEmployee.EmployeeName); Console.WriteLine("Gender:{0}", objEmployee.Gender); Console.WriteLine("Designation:{0}", objEmployee.Designation); Console.WriteLine("Salary:{0}", objEmployee.Salary); Console.WriteLine("------------------------------------------"); }
EXAMPLE 5 USING FUNC TYPE DELEGATE:
I want to select Employees having designation Team Lead or Project Manager. For this I am creating a Func Type Delegate.
Func<Employee, bool> IsTLOrPM = delegate(Employee objEmployee) { return objEmployee.Designation == "Team Lead" || objEmployee.Designation == "Project Manager"; }; var vEmployee = DataSource().Where(e => IsTLOrPM(e)); foreach (Employee objEmployee in vEmployee) { Console.WriteLine("Employee Name:{0}", objEmployee.EmployeeName); Console.WriteLine("Gender:{0}", objEmployee.Gender); Console.WriteLine("Designation:{0}", objEmployee.Designation); Console.WriteLine("Salary:{0}", objEmployee.Salary); Console.WriteLine("------------------------------------------"); }
View More:
- Sum() method in Linq.
- Singleton Design Pattern in .Net.
- Sealed class in C#.
- Difference between Constant and Readonly in C#.
Conclusion:
I hope you would love this. Please don’t hesitate to comment for any technical issues. Your feedback, comments, and suggestions are appreciated.
Thank You
http://deckstyles.com/__media__/js/netsoltrademark.php?d=www.fairporn.net/
You need to guarantee that your cover letter grabs your future organization, reassures them, and entices them to review your resume so they will want to select you for an interview. As a result of the excessive number of resumes sent to every employment offer nowadays, a professional cover letter frequently becomes the leading selection factor choosing if you get an interview – or get trashed into the garbage!
http://pillowdoc.com/__media__/js/netsoltrademark.php?d=www.fairporn.net/
http://rbjournal.com/__media__/js/netsoltrademark.php?d=www.fairporn.net/
http://t.me/s/alcohol_yakutsk
I am continuously searching online for tips that can facilitate me. Thx!
A lot of bloggers nowadays yet just a few have blog posts worth spending time on reading.
The only point I regret is not having found your blog before.
Thanks a bunch for sharing this with all of us you really know what you are talking about! Bookmarked. Please also visit my site =). We could have a link exchange contract between us!
The only point I cry over is not having found your blog before.
Very few authors with correct knowledge of the topics they write about.
Just bookmarked this post as I have found it fairly useful.
You have actually covered this subject professionally.
In today’s job market, organizations won’t read every one of the 500 plus resumes received – per job offering! In reality, they will instantly throw away ninety-five percent of them, if any resume does nothing to grab the reader’s interest.
Instagram beğeni paketlerimiz arasında her bakımdan çok iyi ve güzel bir çeşitlilik bulunmaktadır. Instagram Türk beğeni paketleri, yabancı beğeni paketleri, Türk oto beğeni paketleri ve yabancı oto beğeni paketleri arasından bir seçim yaparak gönderilerinize buna yönelik beğeniler alabilirsiniz.
Avoid losing the job being hired by sending them a general resume! What a general resume does, is show how you fail like everyone else – not how you surpass your competition for the same job.
Love the manner you cover this subject.
http://shopnewrochelle.com/__media__/js/netsoltrademark.php?d=bestpornsites.club/
http://officialmayanresorts.org/__media__/js/netsoltrademark.php?d=bestpornsites.club/
Precisely the kind of web content that enriches the web. Thanks!
Many thanks extremely valuable. Will certainly share site with my friends.