Getting Started With LINQ

This article explains LINQ operations. Language Integrated Query (LINQ) is a set of language and framework features for constructing type-safe queries over local collections or remote data sources. It was introduced in C# 3.0 and .Net Framework 3.5.

The basic unit of data in LINQ is a sequence of elements that actually implements IEnumerable. In the following example, Employees is a sequence and “John”, ”Dave”, ”Black” and ”Jack” are elements.

  1. string[] employees = {"John","Dave","Black","Jack"};   
A query operator is the one that transforms the sequence. A typical query operator accepts a sequence and emits a transformed output sequence. For the IEnumerable case, we have around 40 query operators. They are also known as standard operators.

Let us get started with a few simple queries.

Note: I will use LINQPAD to run these queries. Hence, the final statement dump is specific to it. It's basically used to show the output.
  1. string[] employees = {"John","Dave","Black","Jack","Rahul"};    
  2.      
  3. IEnumerable<string> filtered_emp = employees.Where(n=>n.Length>4);    
  4.      
  5. filtered_emp.Dump("Simple usage of Query");   
run these queries

Since standard query operators are implemented as static methods, we can directly call Where on employees. Most query operators accept lambda expressions as an argument like here we have n=>n.Length>4.

Now, let's look at a complex query.
  1. string[] employees = {"John","Dave","Black","Jack","Rahul"};    
  2.      
  3. IEnumerable<string> query = employees.Where(n=>n.Contains("a"))    
  4.                             .OrderBy(n=>n.Length)    
  5.                             .Select(n=>n.ToLower());    
  6.      
  7. query.Dump("Simple usage of Query");   
complex query

Here, what it is doing is taking the input, extracting all the strings containing the letter "a", then sorting them by length and then converting that to lower case. Here, the variable "n" is privately scoped to each lambda expression. OrderBy and Select are standard query Operators.

Now, let's look at the Range Variable. The Identifier immediately following the from keyword syntax is called the Range variable. For instance, in the following example:
  1. from n in new[] { "John","Dave","Black","Jack","Rahul" }    
  2. where n.Contains ("a")    
  3. select n   
Range Variable

The chaining of the query operator is very efficient in many cases as in the following.
  1. string[] names = { "John","Dave","Black","Jack","Rahul" };    
  2.      
  3. IEnumerable<string> query = names    
  4.     .Where   (n => n.Contains ("a"))    
  5.     .OrderBy (n => n.Length)    
  6.     .Select  (n => n.ToUpper());    
  7.         
  8. query.Dump();    
  9.      
  10. // same query rewritten progressively:    
  11.      
  12. IEnumerable<string> filtered   = names.Where      (n => n.Contains ("a"));    
  13. IEnumerable<string> sorted     = filtered.OrderBy (n => n.Length);    
  14. IEnumerable<string> finalone = sorted.Select    (n => n.ToUpper());    
  15.      
  16. filtered.Dump   ("Filtered");    
  17. sorted.Dump     ("Sorted");    
  18. finalone.Dump ("FinalQuery");    

 

FinalQuery

So, as you can see in the preceding example, the same final query is done in the first expression and again the same thing is done differently by writing it progressively. Similarly, sometimes we need to keep the natural ordering of numbers as it is.

  1. int[] numbers  = { 10, 9, 8, 7, 6 };    
  2.      
  3. numbers.Take (3)  .Dump ("Take(3) returns the first three numbers in the sequence");    
  4. numbers.Skip (3)  .Dump ("Skip(3) returns all but the first three numbers in the sequence");    
  5. numbers.Reverse() .Dump ("Reverse does exactly as it says");    
returns the first three numbers

Now in the end there are other important operators that are equally important.
  1. int[] numbers = { 10, 9, 8, 7, 6 };    
  2.      
  3. // Element operators:    
  4.      
  5. numbers.First().Dump ("First");    
  6. numbers.Last().Dump ("Last");    
  7.      
  8. numbers.ElementAt (1).Dump ("Second number");    
  9. numbers.OrderBy (n => n).First().Dump ("Lowest number");    
  10. numbers.OrderBy (n => n).Skip(1).First().Dump ("Second lowest number");    
  11.      
  12. // Aggregation operators:    
  13.      
  14. numbers.Count().Dump ("Count");    
  15. numbers.Min().Dump ("Min");    
  16.      
  17. // Quantifiers:    
  18.      
  19. numbers.Contains (9).Dump ("Contains (9)");    
  20. numbers.Any().Dump ("Any");    
  21. numbers.Any (n => n % 2 != 0).Dump ("Has an odd numbered element");    
  22.      
  23. // Set based operators:    
  24.      
  25. int[] seq1 = { 1, 2, 3 };    
  26. int[] seq2 = { 3, 4, 5 };    
  27. seq1.Concat (seq2).Dump ("Concat");    
  28. seq1.Union (seq2).Dump ("Union");    
Union

Concat

Thanks for joining me.

Up Next
    Ebook Download
    View all
    Learn
    View all