New C# Features That Support LINQ

Introduction

In this article we will learn some basic concepts of LINQ and apart from that we can see the C# features that support LINQ. This is the basic knowledge of LINQ, we should understand these things perfectly. C# features make the LINQ query easier and more understable. These features are all used to a degree with LINQ queries, they are not limited to LINQ and these features can be used in any context, at what context (beyond) we think it will be useful for us.    

The following is a list of some of the features:

  • Query Expression
  • Object and Collection Initializer
  • Implicitly Typed variable
  • Anonymous types
  • Extension Methods
  • Lambda Expression
  • Auto-implemented Properties

Now let's try to understand all those features one by one.

Query Expression

Before we begin to understand the concept of Query Expression, let's try to understand what a query is.

A Query is a set of instructions that describe what the data is to retrieve from a given data source. And a Query Expression is a query, expressed in query syntax. A LINQ Query Expression is also very similar to SQL. A LINQ Query Expression contains the following three (3) clauses:

  • From
  • Where
  • Select

From: specifies the data source
Where: applies data filtration
Select: specifies the returned elements

Let's understand the Query Expression using the following example.  

  1. var query = from n in numbers  
  2.                   where (n/2)==0  
  3.                   select n;  

Object and Collection Initializer

The Object Initializer helps to assign some value to assessable fields or properties of an object at creation time, without invoking a constructor. This feature enables the specific argument for a constructor. Objects and collection initializers make initilization of objects possible without explicitly calling a constructor for the object. This is typically used in a query expression when they project the source data into a new data type.

Example

  1. Student studentQuery= new Student{ name= "Rajeev" , Address = "Pasir Ris" }  

Implicitly Typed variable

We can use local variables as an inferred "type" of  var  instead of an explicit type. The var keyword instructs the compiler to infer the type of the variable from the expression on the right side of the initialization statement. The inferred type may be a built-in type, an anonymous type, a user-defined type, or a type defined in the .NET Framework class library. A variable decleared as a var are just as strongly-typed as variables whose type you specify explicitly. The use of var makes it possible to create anonymous types.

Example

  1. var num = 5;  
  2. var str="Rajeev";  
  3. var query = from s in StringArray  
  4.                   where s[0] == 'm'  
  5.                   select s;  

Anonymous types

Anonymous types are class types that derive directly from an object and that cannot be cast to any type except objects. It is not different from any other reference type. The complier provides a name for each anonymous type. Anonymous types are typically used in the select clause of a query expression to return a subset of properties from each object in the source sequence. Anonymous types contains one or more public read-only properties. The expression that is used to initialized a property cannot be null.

An anonymous type is constructed by the compiler and the type name is only available to the compiler. It provides a simple way to group a set of properties temporarily in a query result without having to define a separate named type.

Example 

  1. select new { name= student.Name, Address= student.Address};      

Extension Methods

The extension method is a static method that is associated with a type. If we want to call it then this will be called using an instance method syntax. Their first parameter specifies which type the method operates on and the parameter is preceded by the this modifier. An Extension method is only in scope when we explicitly import the namespace into our source code using a using directive. This feature enables us to "add" new methods to an existing type without modifying them. The standard query operators are a set of extension methods that provide LINQ query functionality for any type that implements IEnumerable <T>. 

Lambda Expression

A lambda expression is an anonymous function that we can use to create delegates or expression tree types. By using lambda expressions, we can write local functions that can be passed as arguments or returned as the value of function calls. Lambda expressions are particularly helpful for writing LINQ query expressions. A lambda expression is an inline function that uses the => operator to separate input parameters from the function body and can be converted at compile time to a delegate or an expression tree. In LINQ programming, you will encounter lambda expressions when you make direct method calls to the standard query operators.

Example 

  1. int[] numbers = { 25, 7, 6, 4, 12, 58, 67, 92, 42, 60 };  
  2. int oddNumbers = numbers.Count(n => n % 2 == 1);  

Auto-implemented Properties

Auto-Implemented properties make the property declaration simple where we need not write special logic to define the getter and setter. This feature enables the client code to create objects. When we create the property the complier creates a private field that can be accessed using the property's get and set assessor.

Example 

  1. public string Name { getset;}     

Summary

In this article we have learned the new C# features that support LINQ. These features make the LINQ query more concise and popular in developing prospective things. 


Similar Articles