- Report this article
Amr Saafan
Amr Saafan
Founder | CTO | Software Architect & Consultant | Engineering Manager | Project Manager | Product Owner | +27K Followers | Now Hiring!
Published Apr 21, 2023
+ Follow
Popular computer language C# is used to create a variety of applications, including desktop software and web apps. The “where” keyword, which is used to filter data based on specific criteria, is one of the language’s strong features. In this article, we’ll go over the “where” keyword in depth and give several C# code examples to demonstrate how to utilize it.
The “where” Keyword in C#
The “where” keyword is used in C# to specify a set of conditions that must be met by elements in a collection. This keyword is typically used in conjunction with the “IEnumerable” interface to filter data based on a specific condition.
Here’s an example of how the “where” keyword can be used in C#:
var numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };var evenNumbers = numbers.Where(n => n % 2 == 0);
In this example, we’ve declared a list of numbers, and we’re using the “where” keyword to filter out all the even numbers from the list. The condition we’ve specified is that the number must be divisible by 2 with no remainder.
Recommended by LinkedIn
Using the “where” Keyword with LINQ
The “where” keyword is also commonly used with Language Integrated Query (LINQ) in C#. LINQ allows developers to perform complex queries on data sources using a simple syntax, and the “where” keyword is often used to filter data.
Here’s an example of how the “where” keyword can be used with LINQ in C#:
var books = new List<Book>{ new Book { Title = "The Great Gatsby", Author = "F. Scott Fitzgerald", Year = 1925 }, new Book { Title = "To Kill a Mockingbird", Author = "Harper Lee", Year = 1960 }, new Book { Title = "1984", Author = "George Orwell", Year = 1949 }, new Book { Title = "The Catcher in the Rye", Author = "J.D. Salinger", Year = 1951 }};var modernBooks = from book in books where book.Year > 1950 select book;
In this example, we’ve created a list of books and filtered away all the books that were released before 1950 using the “where” keyword. The query has been specified using the LINQ syntax, which is clearer and shorter than conventional loops.
1
To view or add a comment, sign in
More articles by this author
No more previous content
- Implementing Clickjacking Defense Techniques in JavaScript Sep 15, 2024
- AS Keyword in SQL Server Sep 12, 2024
- Salesforce vs. Dynamics: Which CRM Solution Is Best for Your Business? Sep 11, 2024
- Scaling React Apps with Reusable List Components Sep 10, 2024
- How to Use the SQL Server ANY Keyword for Flexible Querying Sep 9, 2024
- Advanced Apex Techniques for Salesforce Developers Sep 4, 2024
- Terraform Your Way to High Availability: Deploying a Full Stack AWS Architecture Sep 3, 2024
- 10 Reasons TypeScript is Transforming How We Build Web Apps Sep 2, 2024
- Django Request Life Cycle Explained Sep 1, 2024
- C# .NET Exception Handling: Why You Should Avoid Using throw ex in Catch Blocks Aug 31, 2024
No more next content
Sign in
Stay updated on your professional world
Sign in
By clicking Continue to join or sign in, you agree to LinkedIn’s User Agreement, Privacy Policy, and Cookie Policy.
New to LinkedIn? Join now
Insights from the community
- Language Integrated Query (LINQ) How do you control LINQ query execution?
- Web Applications How do you use web application APIs to visualize data?
- Software Development How do you choose between JSON and XML for data exchange?
- Software Development How do you measure the performance of a parallel LINQ query?
- Language Integrated Query (LINQ) How do you change the execution context of a LINQ query expression?
- Language Integrated Query (LINQ) How do you compare and contrast different LINQ providers and extensions?
- Web Development How can you use Sequelize ORM in Node.js?
- Computer Science What are the best ways to prevent SQL injections in web design?
- Data Entry How do you handle complex or nested data structures with JSON?
- Data Processing What are the advantages and disadvantages of using JSON vs XML?
Others also viewed
- How To Optimize An SQL query in a Django application? Amr Saafan 1y
- Out of the Shadows: Explore a Database Projects’ Inner Workings using SSDT Schema Compare Dan Romano 1y
- What is XML : Why We Can Use It Best explanation With Example on 2024 Nexotips Infotech 4mo
- All About LINQ Queries in C#: A Comprehensive Guide Asharib Kamal 1mo
- GUID vs ULID in .NET C# Ashraf Faheem 2mo
- Laravel Eloquent One-To-One Relationship - Ultimate Guide 2023 Mahmoud Adel 1y
- C# Keywords Tutorial Part 10: by Amr Saafan 1y
- How to Make Your LINQ Extensions Reusable for Other Developers Peter Smulovics 1y
- Understanding require function (Node.js) Matias Daniel Torre 11mo
- Why LINQ beats SQL RAUNAK DEEP SRIVASTAVA 8y
Explore topics
- Sales
- Marketing
- IT Services
- Business Administration
- HR Management
- Engineering
- Soft Skills
- See All