LINQ allows us to query data in a application using a .NET language such as C# similar to how SQL is used for querying databases.
Introduction to LINQ queries
Linq queries are composed using the query operators.There are many query operators defined in the System.Linq namespace as extension methods.These query operators are defined as extension methods on the Enumerable and Queryable classes.
Query operators defined on the Queryable class are used to execute queries on a remote data source such as SQL Server database whereas the query operators in the Enumerable class are used for executing queries for in memory collections.
The Standard Query Operators are divided into two groups one which operates on a sequence of type IEnumerable<T> and other which operates on sequence of type IQueryable<T>.
Some of the most useful query operators are:
from this clause begins the query and must precede all other clauses such as select and where.the from clause specifies two important aspects of a query.It specifies the source and a range variable which represents each item in the source.It is used as:
var variable=from rangeVariable in Source
The type of the range variable is automatically inferred from the type of elements in the source.So if the source is of type List<Student> then the range variable will be of type Student.
Where used for filtering a collection based on a condition.The condition which is specified as a predicate is evaluated against each item in the collection.If the condition returns true then the item is included in the result collection.
The function passed to the Where operator is of the form Func<TSource, bool>.This is a predicate function as it returns a boolean value.A value of true or false is returned which depends on the item in the collection.It is used as:
where boolean condition
The boolean condition above is a predicate which is applied to each element in the data source.If the condition returns true then the element is included in the result.
Select used for projecting a collection depending on the function passed in to each item of the collection.The function passed to the select operator is of the form Func<TSource, TResult> .This function accepts an input of a specific type and returns the transformed type.It is used as:
var result= from rangeVariable in DataSource where boolean condition select rangeVariable.Property
Example of LINQ query
We have declared a list of countries and each country has an associated continent.
List<Country> countries =new List<Country>(){ new Country{Name="Japan",Continent="Asia"}, new Country{Name="Singapore",Continent="Asia"}, new Country{Name="Australia",Continent="Australia"}, new Country{Name="USA",Continent="America"}, new Country{Name="New Zealand",Continent="Australia"} };
To filter the country which belongs to Asia we can use the either of the two queries.
1)Following query uses the query operators to filter the list of asian countries
IEnumerable<string> asianCountries = countries.Where(item => item.Continent == "Asia").Select(item => item.Name);
2)We can write the above query using query expression syntax as:
IEnumerable<string> asianCountries= from country in countries where country.Continent == "Asia" select country.Name;
We have both the options available when writing a Linq query.If we write a Linq query using a query expression syntax then it gets translated to query operator syntax in the IL compiled code.
Once we have declared the query we can display the result by enumerating over the collection as:
foreach (var item in asianCountries) { Console.WriteLine(item); }
This was just an introduction to LINQ.There are many useful operators which we can use when writing a LINQ query.Some of the useful operators are:
- GroupBy For grouping elements
- Join For joining two data sources
- SingleOrDefault Returns a single element satisfying a condition
- Average Returns average value of a collection of values
- Count Returns the count of elements in a collection
These operators simplifies otherwise complex data access operations.