LINQ basics operators and concepts

Basics on LINQ. Helpful article to get you started with LINQ. Discussion on some commonly used operators when we implement LINQ in our project. Can be helpful in avoiding redundancy and making code compact and maintainable.

Some basics to get you started with LINQ(Language Integrated Query).

Is LINQ a language?

NO. LINQ is just a query syntax and not a language. It makes our code cleaner and compact. It can be thought of as a replacement to 'for loop'. (I am not talking about LINQ to SQL here as it is a different concept altogether.)

First of all try to understand what are Extension Methods.

Extension Methods

Extension methods are static and it allows us to extend or improve a type. Mark the first parameter with the 'this' keyword:

public static class ExtensionsMethodExample
public static string TestExtension(this String str)
str = "Choose .NET java PhP";
str = "Choose DB,Testing";
return str;

//Now, call the extension method 'TestExtension'

var strCareer = "I want to be a Programmer";
strCareer = strCareer.TestExtension();

Simple Example Implementing LINQ on our List

string[] paths = {"ASP.NET","Java","PhP", "Oracle"};

var strCareer = "I love ASP.NET";

var strChoose = paths.Where(x => x.Contains(strCareer));//used Lambda Expression here.

What are Lambda Expressions.

A more concise way of writing an Anonymous methods. The '=>' operator can be read as "goes to" and it is always used when declaring a lambda expression

P => list.count>3

The list array with count > 3 will be allocated to p in Enumerated fashion.

Basic Query Operators in LINQ

In LINQ, the data is first filtered(WHERE operator) then Sorted(OrderBy Operator if any) and then Projected(SELECT operator).

string[] paths = {".NET","C#.NET","PhP", "VB.NET"};

var strCareer = "I love .NET";

var strChoose = paths.Where(x => x.Contains(strCareer))
.OrderBy(x=> x.Length)
.Select(x=> x.ToLower());

Other Query operators in LINQ

We will take a look at Take, Skip, Reverse, Aggregation, Quantifier operators in LINQ.

Consider the example of an Integer Array

Int[] num = {100,200,300,400,500};

Takes the number of elements specifies and ignores the rest.

IEnumerable takeFirstThree = num.Take(3);
// OUTPUT: { 100, 200, 300 }

Skip: Ignores the first 'n' elements and takes the rest.

IEnumerable skipFirstThree = num.Skip(3);
// OUTPUT: { 400, 500 }

Reverse: Reverses the entire array.

IEnumerable revList = num.Reverse();
// OUTPUT : { 500, 400, 300, 200, 100 }

Aggeration: Returns a single(scalar) value.

int count = num.Count(); // Output = 5;
int min = num.Min(); // Output = 100;

Quantifiers: Returns a bool value.

bool hasTwoHundred = num.Contains (200); // true
bool hasElements = numbers.Any(); // true

The 'into' keyword

'into' restarts the query. Allows fresh Where, OrderBy and Select Clauses.

IEnumerable query = from p in paths
select p.Replace (".NET", "DOT NET").Replace ("Oracle", "Oracle 11g").Replace ("Java", "Java/J2EE")
into intoKeyword
where intoKeyword.Length > 3 orderby intoKeyword select intoKeyword;


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: