LINQ (Language integrated query )


 

LINQ (Language integrated query )

LINQ (Language integrated query )

LINQ (Language integrated query )

LINQ is a powerful set of technologies based on integrating query capabilities into c# language.

The LINQ provides a consistent query experience to query objects (LINQ TO OBJECTS)  relational databases (LINQ to SQL) and XML(LINQ to XML).

LINQ is uniform query syntax in c# to retrieve data from different sources and formats such as collections, ADO.NET Dataset    ML Data web server, ms SQL and other databases

LINQ query return as Objects

LINQ query can be executed in multiple ways, use a 'for each' or 'for' loop to traverse the collection to find a particular object  



Example: LINQ query to an array 


// Data source

string[] names = {"Bill", "Steve", "James", "Mohan" };


// LINQ Query 

var myLinqQuery = from name in names

                where name.Contains('a')

                select name;

    

// Query execution

foreach(var name in myLinqQuery)

    Console.Write(name + " ");

Use to for loop is cumbersome, not maintainable and readable, c# introduced Delegate.

LINQ makes code more compact and readable and it can also be used to query different data sources.


ADVANTAGES OF LINQ -

familiar language, less coding, readable code, and a standardized way of querying multiple data sources.

Compile time safety of queries, shaping data. 


In LINQ API, we can write LINQ queries for the classes that implement the IEnumerable, IQueryable<T> interface and it contains extension methods to write LINQ queries.

Use the System. Linq namespace to use Linq.

LINQ includes to main static class enumerable and queryable.


Query syntax - 
LINQ (Language integrated query )

It is similar to SQL (Structured Query Language) for the Database and it is defined within C#.

Linq Query syntax always ends with a select or Group Clause.

Use various other operators like filtering, joining, grouping, and Sorting operators to construct the desired result.

Method syntax is like calling the extension method.

The Compiler Converts query syntax into method syntax, at compile time.

Example  - Multiple select and where the operator 

var studentNames = studentList.Where(s => s.Age > 18)

                              .Select(s => s)

                              .Where(st => st.StandardID > 0)

                              .Select(s => s.StudentName);

 

If you found this article useful, share it.*

MS SQL Server interview questions for developers ~ SoftCodeLearner

SoftCodeLearner: ASP.NET

BookStore ~ SoftCodeLearner

FACEBOOK


Join telegram group-https://t.me/softwareCodeLearner

No comments:

Post a Comment