Introduction to LINQ
Language Integrated Query (LINQ) is a query syntax that defines a set of query operators that allow traversal, filter, and projection operations to be expressed in a declarative way in any .NET-based programming language. It provides a unified programming model for querying and updating data from different types of data sources and extends data capabilities directly into the C# and Visual Basic languages. LINQ simplifies the interaction between object-oriented programming and relational data by applying the principles of object-oriented programming to relational data.
With the advent of LINQ, a ground-breaking, new concept of a query has been introduced as a first-class language construct in C# and Visual Basic. LINQ simplifies the way you work with data queries. LINQ offers you a unified, declarative syntax model to query any data source including an XML document, SQL database, an ADO.NET Dataset, an in-memory collection, or any other remote or local data source that chooses to support LINQ Language Integrated Queries are strongly typed and designer tools can be used to create object-relational mappings. It is easier now for developers to catch many errors during compile-time; also supports Intellisense and debugging.
Sample Query 1:
A query is an expression that retrieves data from a data source. All LINQ query operations consist of three essential actions: Obtain the data source,Create the query and Execute the query.
In LINQ, the execution of the query is isolated from the query itself and hence data cannot be retrieved just by creating a query;
In the following sample, a query retrieves even numbers from an array of integers.
// Data source. int[] numbers = new int[10] { 0, 1, 2, 3, 4, 5, 6 ,8,9,10};
// Query creation. IEnumerable numQuery = from num in numbers where (num % 2) == 0 select num;
// Query execution. foreach (int j in numQuery) { Console.Write("{0,1} ", j); }
Output will be: 0,2,4,6,8,10
Sample Query 2:
string[] names = { "Baskar", "Babu", "Bala", "Balu", "Banu" };
Once you have a collection of objects, as an array defined above, you may apply LINQ expressions to query whichever data you want from the collection.
IEnumerable expr = from a in names where a.Length == 4 orderby a select a.ToUpper(); foreach (string name in expr) Console.WriteLine(name);
The above query returns the names in uppercase with length only 4 characters. So, the output will be,
BABU BALA BALU BANU
The funda behind Func variable:
Func is a delegate included in C# versions 3.0 and later to provide easy way to associate a method, its return type to a delegate. You have no need to declare a delegate type explicitly and then associate it to a method to call. In fact, the underlying type of any Lamda Expression is of Func type. It takes the following form...
public delegate TResult Func (T arg);
where T and TResult is a type used for the argument and return data respectively.
There are many other forms of this Func delegate but it is not the scope of this article.
Sample Query 3:
Optionally, Func delegate can be used to define filter condition, projection and extraction of data from a collection with in a query as below.
Console.WriteLine("Filtered names:"); Func filter = a => a.StartsWith("Bal"); Func project = a => a.ToUpper(); Func extract = a => a; IEnumerable expr1 = names .Where(filter) .OrderBy(extract) .Select(project) .Skip(1); foreach (string name in expr1) Console.WriteLine(name);
There are two names starting with ‘Bal’ namely “BALA” and “BALU”. But, the above query returns the results skipping one name before displaying the output. So the final output will be,
Conclusion:
The above article explains the basics of writing queries in LINQ.
|
No responses found. Be the first to respond and make money from revenue sharing program.
|