LINQ tutorial Part -Ist

1. What is Linq ?

Ans :- LINQ is stands for Language Integrated Query.Its new set of technologies which provide simple statement like sql. On any object.

This just like simple Sql statements. Linq can be operational on Object, SQL and XML.

LINQ TO Object :- Linq to object allow you to query on collection of objects like array

LINQ To Sql :- by the name its clear that it will work on Data.With help on ORM we can use data to manipulate and access the data.

LINQ To XML :-One of the most important feature of linq is that it can be operational on XML too. With the help of this we can use XML in easy way for read & Write Operation.

2. Linq Operations :-

Linq is such utility which can be used by programmer in very easy way. There are many operation which we can use like select data, conditional selection, sum, average other operation can be perform on object with linq. All be below operation is applied on Sql & XML also.

Here I am with a array on which we do linq operation

Dim ah As Integer() = {10, 4, 6, 20, 55, 22, 1, 40, 66, 69, 77, 24, 20, 10, 33}

1) Simple Select :- it will return all the element of array

Ex:- dim lnq = from a in ah select a

2) Where clause :- The where clause work in below manner

Ex:-

Dim lnq = From a In ah Where a < 20 Order By a

With this we can find array element which is less then 20

Dim lnq = From a In ah Where a Mod 2 = 0 Order By a

with this statement we get element only which is fully divisible by 2

Dim lnq = From a In ah Where a = 66

With this we get only value 66

3) And clause :-

Ex:- Dim lnq = From a In ah Where a >= 30 And a <= 69 Order By a

with this we can get only those element which is in the range of 30 & 69

4) Or clause :-

Ex- Dim lnq = From a In ah Where a >= 30 Or a Mod 2 = 0 Order By a

With this we can get element in or condition means either element may be greater than 30 or modules with 2 will be 0.

5) Take clause :- take (number) with this we can get particular number of element from array which we specify

Ex:-

Dim lnq = From a In ah Select a Take 3 Order By a Descending

With this element we can get starting 3 element in array

6)Take With while condition :-

This is same the take clause means it also get selected element but only one thing is here up to which condition is true

Ex:-

Dim lnq = From a In ah Take While a Mod 2 = 0

This will take all the starting element from array whose modules 2 is zero if it breaks after 2 element then take statement terminate.

7) Skip:- It is just opposit to take it . It skip the number of elements from starting position as assign.

Ex:-

Dim lnq = From a In ah Skip 3

This will skip first 3 element from array

8) Skip with While:- It Will skip the element up to true condition

Ex:- Dim lnq = From a In ah Skip While a Mod 2 = 0

This will skip all the starting element from array whose modules 2 is zero.

9) Order by:- if we want to order by the linq then we can do this by order by. By default its ascending which can be change by writing descending after oder by if we want to oder by on second criteria using ThenBy we can use.

 

10) Reverse clause:- Reverse It reverse the selected records from linq

Ex:-

Dim lnq = (From a In ah Select a).Reverse()

it give us revese array elements

11) Distinct clause :-

Distinct operation give us distict element from linq.

Ex:- Dim lnq = From a In ah Select a Distinct

This will give distict element from array.

12) First clause :-First Clause give you first element of the linq.
Ex:-Dim lnq = (From a In ah Select a).First()
it Will give first element of the array.
13) Last clause :-

Its just opposite to first clause it will give last element of the linq.

Ex:-Dim lnq = (From a In ah Select a).Last()
it will give the last element of the array.

14) FirstOrDefault :-

if there is no value in selection then it will return the default value other wise it will return first Element.

Ex:-

Dim lnq = (From a In ah Select a).FirstOrDefault()

it Will give first element of the array.

15) LastOrDefault :-

if there is no value in selection then it will return the default value otherewise it will return the last element.

Ex:-

Dim lnq = (From a In ah Select a).LastOrDefault()

it Will give Last element of the array and if array don’t have any value then it will return zero.

16) ElementAt Clause :-

Element At particular index position & elementAtDeafult(position)

Ex:-

Dim lnq = (From a In ah Select a).ElementAt(16)

it will return element at index position 16

17) Count :-

It will give the number of element selected by linq.

Ex:-Dim lnq = (From a In ah Select a).Count()
18)Sum :- It will give sum of linq element.
Ex:-Dim lnq = (From a In ah Select a).Sum() 
19) Min:- It will return minimum from linq.
Ex:-Dim lnq = (From a In ah Select a).Min()
20) Max:-It will return from max from linq
Ex:-Dim lnq = (From a In ah Select a).Max()
21) Average :- it will give the average from linq .
Ex:-Dim lnq = (From a In ah Select a).Average()
Rest will be in next post.

Thanks

Rajat

Advertisements

2 thoughts on “LINQ tutorial Part -Ist

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s