Thursday 23 May 2013

LINQ

LINQ is a technique for querying data from any Datasource. data source could be the collections of objects, database or XML files. We can easily retrieve data from any object that implements the IEnumerable<T> interface.

Advantages:  as Linq Queries is integrated with .net c# language , it enables you to write code much faster then than if you were writing oldstyle queries. In some cases I have seen, by using LINQ development time cut in half.

Microsoft basically divides LINQ into three areas and that are give below.
  1. LINQ to Object {Queries performed against the in-memory data}
  2. LINQ to ADO.Net
    A- LINQ to SQL (formerly DLinq) {Queries performed against the relation database only Microsoft SQL Server Supported}
    B- LINQ to DataSet {Supports queries by using ADO.NET data sets and data tables}
    C- LINQ to Entities {Microsoft ORM solution}
  3. LINQ to XML (formerly XLinq) { Queries performed against the XML source}
now we write some code snippet of LINQ.

int[] nums = new int[] {0,1,2};

var res = from a in nums
             where a < 3
             orderby a
             select a;

foreach(int i in res)
    Console.WriteLine(i);

Output:
0
1
2

Return type of Linq
its depends on the select operation.
If you are querying a database(Linq to SQL) then the return type is IQueryable<T> where T is Product in below
var product = from p in dbContext.Products
             
select p;
If you are using Linq againist a datasource which is an Enumerable collection(List) then the return type will be IEnumerable<t>

0 comments:

Post a Comment

Topics

ADFS (1) ADO .Net (1) Ajax (1) Angular (43) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (47) CD (1) CI (2) CloudComputing (2) Coding (7) CQRS (1) CSS (2) Design_Pattern (6) DevOps (4) DI (3) Dotnet (8) DotnetCore (16) Entity Framework (2) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) Lamda (3) Linq (11) microservice (3) Mongodb (1) MVC (46) NodeJS (8) React (11) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (1) UI (1) UnitTest (1) WCF (14) Web Api (15) Web Service (1) XMl (1)

Dotnet Guru Archives