Common LINQ Operations
Learn to use common LINQ operations to query and manipulate collections in C#. Understand filtering with where clauses, sorting with orderby, projecting custom results, and combining multiple data sources. Explore both query syntax and method syntax to write efficient, readable code.
We'll cover the following...
Most LINQ queries written using query syntax follow a predictable structure:
var result = from item in collectionselect item;
Line 1: We define the data source (
collection) and a range variable (item) that represents each individual element during iteration.Line 2: We use the
selectstatement to determine exactly what the query returns.
This basic syntax can be extended with filtering, sorting, projection, and other operations, which we explore individually.
Filter
To select items that match specific criteria, we use the where clause. After the where keyword, we provide a boolean expression that determines how the filtering occurs.
Line 1: We import the
System.Collections.Genericnamespace to access theList<T>collection type.Lines ...