Showing posts with label learn linq step by step. Show all posts
Showing posts with label learn linq step by step. Show all posts

Tuesday, 29 October 2013

LINQ to Stored Procedure(without parameter)

For Advance Asp.net Training Fill Enquiry Form
LINQ to Stored Procedure
(Without Parameter)

LINQ also provide facility to connect with Stored Procedure. Following is the table which I am using to explain this topic.

Table Name:-Emp


Figure 1

To work with LINQ to SQL we have to add LINQ Classes. Using Add New Item add LINQ to SQL Classes.



Figure 2

Now create class to map with Table Emp

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq.Mapping;

[Table(Name="Emp")]
public class Class1
{
    //IsDbGenerated represents that it is Identity column
    [Column(IsPrimaryKey=true, IsDbGenerated=true)]
    public int id;
    [Column]
    public string name;
    [Column]
    public int age;
}

Follow the following steps:-

Step 1:-

Create Stored Procedure

create  proc selectAllData
as
select * from emp

Step 2:-

To work with stored procedure in LINQ we have to use DataContext class. To run stored procedure we have to use ExecuteMethodCall which is protected type. So we have to first inherit the DataContext Class to call the method ExecuteMethodCall ().

Monday, 7 October 2013

Set Operations in LINQ

Set Operations in LINQ

We use following set operations in LINQ:-

Distinct

Distinct remove redundancy and show the unique records.



Figure 1

For example:-

int[] techaltum = { 1, 2, 3, 4, 3, 4, 5, 19, 89, 4, 8, 2 };
        IEnumerable<int> res = techaltum.Distinct();
        Response.Write("Result after Distinct <br/>");
        foreach (int data in res)
        {

            Response.Write(data+"<br/>");
              
        }

Sunday, 18 August 2013

First and Last Method in LINQ

Written By:-Isha Malhotra 
Our Website:-www.techaltum.com    

First and Last in LINQ

Use of First in LINQ

First method in LINQ just select the top 1 value from the data source.

For Example:-

First with array

int[] marks = new int[] { 23, 45, 78, 45, 56, 89, 56, 32 };

       int first = marks.First();

       Response.Write("First Element " + first);
Ouput



We can also pass condition while calling first method

Thursday, 25 July 2013

Jagged array using LINQ

Written By:-Isha Malhotra 
Our Website:-www.techaltum.com                                         
Jagged array using LINQ


We can also traverse jagged array using LINQ.

Following is the jagged array:-

int[][] jarray =new int[3][];
        jarray[0] = new int[3] { 4, 5, 6 };
        jarray[1] = new int[2] { 7, 8 };
        jarray[2] = new int[4] { 9, 10, 34, 12 };

Suppose I have to show all even elements then we will
Traverse the jagged array using LINQ in following manner:-

int[][] jarray =new int[3][];
        jarray[0] = new int[3] { 4, 5, 6 };
        jarray[1] = new int[2] { 7, 8 };
        jarray[2] = new int[4] { 9, 10, 34, 12 };

        IEnumerable<int> res = from jar in jarray
                               from res_jar in jar
                               where res_jar % 2 == 0
                               select res_jar;

        foreach (int res_data in res)
        {

            Response.Write(res_data);
       
        }

Saturday, 13 July 2013

Use of long count in LINQ

Written By:-Isha Malhotra 
Our Website:-www.techaltum.com
Use of long count in LINQ

Long count is similar to count as it works same but the only difference between them is that long count returns a 64 bit integer.

For Example:-

int[] marks = new int[] { 23, 45, 78, 90, 56, 89, 10, 32 };

      Int64 res = marks.LongCount(x => x % 2 == 0);
                        OR
long res_long = marks.LongCount(x => x % 2 == 0);
       Response.Write(res_long);

The output of this code will be 5.

Saturday, 29 June 2013

LINQ Hour 5-3 Skip/SkipWhile and Take/TakeWhile in LINQ

Written By:-Isha Malhotra 
Our Website:-www.techaltum.com
Skip/SkipWhile and Take/TakeWhile
Skip

Skip in LINQ is used to skip values from the beginning. We pass a numeric value to skip which represent that how many values we need to skip.

For example

Skip with array

int[] marks = new int[] { 23, 45, 78, 90, 56, 89, 10, 32 };

        var data_skip = (from res_skip in marks
                         orderby res_skip
                         select res_skip).Skip(3);
        Response.Write("Result using skip and skip count is 3 <br/>");
        foreach (int res_data_skip in data_skip)
        {
       
            Response.Write(res_data_skip+" ");
       
        }
Output



Figure 1

It simple arrange in ascending order and then skip 3 records as we gave 3 as input in the skip.

Friday, 14 June 2013

LINQ Hour 5-2 Sorting in LINQ

Written By:-Isha Malhotra 
Our Website:-www.techaltum.com
Sorting in LINQ

To sort data in LINQ we use orderby in the following manner:-

Sorting with Array

int[] marks = new int[] { 23, 45, 78, 90, 56, 89, 10, 32 };

        // it will sort in ascending order
        var sort_asc = from res_asc in marks
                       orderby res_asc
                       select res_asc;

        //it will sort in descending order
        var sort_desc = from res_desc in marks
                        orderby res_desc descending
                        select res_desc;

        Response.Write("Ascending order result <br/>");
        foreach (int res_asc in sort_asc)
        {

            Response.Write(res_asc + " ");
       
        }


        Response.Write("<br/>descending order result <br/>");
        foreach (int res_desc in sort_desc)
        {

            Response.Write(res_desc + " ");

        }

The output of the code is as follows:-



Figure 1

LINQ Hour 5 -1 Aggregate function in LINQ

Written By:-Isha Malhotra 
Our Website:-www.techaltum.com
Aggregate function using LINQ

We use many aggregate functions like sum, count, average, min, and max. In LINQ we can use this function in the following manner:-

SUM

If we want to perform sum aggregate function then we will implement the following queries:-

Sum with array

int[] marks = new int[] { 23, 45, 78, 90, 56, 89, 10, 32 };

        //simple sum without condition

        int res = marks.Sum();

        //sum with condition
        int res_with_where = marks.Where(cond => cond > 50).Sum();

      

        Response.Write("Sum without condition= " + res + "<br/>");

        Response.Write("Sum with condition="+ res_with_where+"<br/>");

Output of this code as follows:-



Figure 1

LINQ Hour 5 - Queries using LINQ

Written By:-Isha Malhotra 

Our Website:-www.techaltum.com
Perform different functionality (QUERY) using LINQ

As in the previous article I already discussed about select, insert, update and delete. I also discussed with where clause, order by, grouping. In this hour I will discuss about different operation which we can perform using LINQ. As we know we can perform this functionality on array, generics, database, xml etc. so here I am taking the example of array, generics and database. I will discuss xml separately.  

To work with this functionality I am taking the following source:-
Array

Using following int array:-

int[] marks = new int[] { 23, 45, 78, 90, 56, 89, 10, 32 };

Generics:-

Using following class to implement generics:-

public class data
{
    public int roll_no;
    public string student;
    public int per;

}

And following list:-

List<data> dt = new List<data>()
        {
            new data{roll_no=1, student="isha", per=100},
            new data{roll_no=2, student="neha", per=89},
            new data{roll_no=3, student="rahul", per=34}
        };
Database

Using following table:-



Figure 1

And create its class to mapping this table which is as follows:-

[Table(Name="prod_rep")]

public class Class1
{
   
    [Column]
    public int id;
    [Column]
    public int prod_year;
    [Column]
    public string dept;
    [Column]
    public int no_of_prod;
}

click on the following link to perform different functionality:-

Part 1 Aggregate function in LINQ

Part 2 Sorting in LINQ

Part 3 Take / TakeWhile and Skip / SkipWhile