0% found this document useful (0 votes)
44 views15 pages

LINQ

Uploaded by

Geetha Smiley
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views15 pages

LINQ

Uploaded by

Geetha Smiley
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

LINQ

What is LINQ
 LINQ stands for "Language Integrated Query”.

It enables us to query the data from the various data sources like SQL
databases, XML documents, ADO.NET Datasets, Web services and any
other objects such as Collections, Generics etc.
 using a SQL Query like syntax with .NET framework languages like C#
and VB.NET.
Advantages of LINQ
 Writing
codes is quite faster in LINQ and thus
development time also gets reduced significantly.

 LINQmakes easy debugging due to its


integration in the C# language.
Why LINQ
 LINQ also provides a uniform programming model (i.e.
common query syntax) to query various data sources.

 It has full type checking at compile-time and This powerful
feature helps you to avoid run-time errors.

 It supports various powerful features like filtering, ordering


and grouping with minimum code.

 Its Query can be reused.


The query expression has three clauses.
 from,
where
and select.

The from clause describes the data source.


The where clause applies filters
and the select clause produces the result of the query
by shaping the data.
int[] numbers = { 5, 4, 1, 3, 9, 8};

var ch =from n in numbers //data source

where n > 3 //condition

select n; //selection

foreach (var i in ch)


{
Console.WriteLine(i+" ");

}
}

// Output: 4 5 8 9
using System;
//Step2: Query
using System.Collections.Generic;
//LINQ Query using Query Syntax
using System.Linq;
to fetch all numbers which are > 5
namespace LINQDemo
var res = integerList.Where(x=x>
{
5).ToList();
class Program
{
//Step3: Execution
static void Main(string[] args) foreach (var item in res)
{ {
//Step1: Data Source Console.Write(item + " ");
List<int> integerList = new List<int>() }
{
1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
}; }
}
string[] names = {"Bill", "Steve", "James", "Mohan" };

// LINQ Query

var result = from name in names


where name.Contains('a')
select name;

// Query execution

foreach(var name in result)


Console.Write(name + " ");
Public static void main()
var
{ r = list.Where(a=> a.Contains(“kum
ar "));

List<string> list = new


List<string>() foreach(var i in r)
{
{ Console.WriteLine(i);
" ram kumar, }
}
" anil kumar ", }
" santhosh ",
" pawan "
};
 syntax of LINQ

 There are two syntaxes of LINQ. These are the following ones.

 Lamda (Method) Syntax

 var longWords = words.Where( w ⇒ w.length > 10);


Query (Comprehension) Syntax

 var longwords = from w in words where w.length > 10;


LINQ to SQL

execution architecture of LINQ to


SQL.
Query operators

You might also like