Dot Net For All

Prerequisites to learn LINQ with C#

Hello friends, I have been writing some article about the LINQ and its use cases. In this article I will walk you through some of the prerequsites to learn LINQ with C#.

Prerequisites to learn C# LINQ

Language Integrated Query or most commonly known as LINQ was introduced in C# 3.0 But it was not that LINQ can exist on its own. There were some other .NET specific features which were introduced prior to LINQ.

LINQ is cumulative outcome of all of these features. Below is the comprehensive list of all of these features: You should be aware of all of the below language features before stating to learn LINQ.

Below is the brief description of these features

Implicitly typed local variables

Implicitly typed local variables are used to get the type of local variable based on the expression or type of literal values assigned to the variable.

Basically there is no need to initialize a variable with the any of the primitive types or user defined type.

var myName = "Vikram"

The type of myName variable will be determined at compile time by the compiler.

These are very helpful if we are working with anonymous type in LINQ. In case of anonymous type we would not be aware of the type and hence it is difficult to guess the type of the assigned variable.

Below are some more examples of the implicitly types local variables.

var i = 12;
var s = "Hello";
var d = 1.0;
var numbers = new[] {1, 2, 3};
var process = new ProcessData();
var processes =
  new Dictionary<int, ProcessData>();

In VB.NET it is equivalent of dim keyword.

Object initializers in C#

Object initializers in C# allow you to initialize an object at the time of creation. This is syntactically very helpful and a one of the must know Prerequisites to learn LINQ.

Let’s have a quick C# example of the same

public class Person
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

Generally we use the below code to initialize an object of Person class.

Person myPerson = new Person();
myPerson.ID = 1;
myPerson.FirstName = "Vikram";
myPerson.LastName = "Chaudhary";

But the above code would not be helpful if we are working with LINQ.

With the help of object initializer the same code can be written as below:

var person = new Person() { ID = 1, FirstName = "Vikram", LastName = "Chaudhary" };

Lambda expressions in C#

Lambda expression or arrow functions as called in some other programming languages eases the way we write methods or functions. These are highly useful feature and one more Prerequisites to learn LINQ.

I have already written a very detailed post about the lambda expression and anonymous function. Please have a look.

Extension methods

Extensions methods can help to write additional methods without changing the class itself.

These are very useful while dealing with LINQ as most of the features of LINQ are covered in extension methods.

You can go though one of post on my blog to know about extension methods.

C# Anonymous types

Anonymous types are used to create the class at run time. Anonymous types use object initilizer to create the objects at run time.

Below is an example of anonymous type.

var student = new { Id = 1, FirstName = "Vikram", LastName = "Chaudhary" };

There is an article on which covers C# anonymous types extensively.

Conclusion:

My main idea of this article was to walk you through the Prerequisites to learn LINQ and help you to start with LINQ. Some of these are already covered in detail somewhere on my blog others I will cover up pretty soon in near future.

Top career enhancing courses you can't miss

My Learning Resource

Excel your system design interview