Monday, September 6, 2010

LINQ - Linq to Object Part 2

Linq to Object Part 1

Continue with above example.

Step 1
WHERE CLAUSE
Add a GetGivenStudentData function in LinqToObject class in App_code folder,it is look like this


 /// <summary>
    /// Where Clause
    /// </summary>
    /// <param name="ListObj">Specify Student List Object</param>
    /// <returns>List</returns>
    public static List<Student> GetGivenStudentData(List<Student> ListObj)
    {
        try
        {
            var Query = (from Q in ListObj
                         where Q.FirstName == "Ramdas"
                         select Q).ToList<Student>();

            return Query; 
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);  
        }
    }

Bind the Data in GridView,it is look like this


//// Display Given Student Data -- WHERE Condition
     GvStudent.DataSource = LinqToObject.GetGivenStudentData(AddData());
     GvStudent.DataBind();  

Step 2
Start With
Find a Student name which beginning with the letter 'k'.it is look like this


 /// <summary>
    /// Start with Letter with Where Clause
    /// </summary>
    /// <param name="ListObj">Specify Student List Object</param>
    /// <returns>List</returns>
    public static List<Student> GetStartwithStudentData(List<Student> ListObj)
    {
        try
        {
            var Query = (from Q in ListObj
                         where Q.FirstName.StartsWith("k")
                         select Q).ToList<Student>();

            return Query; 
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);  
        }
    }

Bind the Data in GridView,it is look like this

////// Start with - Where Clause
            GvStudent.DataSource = LinqToObject.GetStartwithStudentData(AddData());
            GvStudent.DataBind(); 

Step 3
ORDER BY ASCENDING
The sorting functionality is achieved by using the orderby opertaor.it is look like this


 /// <summary>
    ///  Order By ascending
    /// </summary>
    /// <param name="ListObj">Specify Student List Object</param>
    /// <returns>List</returns>
    public static List<Student> GetStudentDataSortAscending(List<Student> ListObj)
    {
        try
        {
            var Query = from Q in ListObj
                        orderby Q.FirstName ascending 
                        select Q;

            return Query.ToList<Student>();   
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);  
        }
    }

Bind the data in GridView,it is look like this


 ////// Order by ascending
            GvStudent.DataSource = LinqToObject.GetStudentDataSortAscending(AddData());
            GvStudent.DataBind();

The sorting can be ascending or descending. By default the sorting will be ascending on the field name specified by orderby operator. 

Step 4
ORDER BY DECENDING
If we want to order them in descending order we need to explicitly use the descending keyword followed by the field name on which we want to apply the ordering.it is look like this



 /// <summary>
    /// Order by descending
    /// </summary>
    /// <param name="ListObj">Specify student List Object</param>
    /// <returns>List</returns>
    public static List<Student> GetStudentDataSortDescending(List<Student> ListObj)
    {
        try
        {
            var Query = from Q in ListObj
                        orderby Q.FirstName descending
                        select Q;

            return Query.ToList<Student>();    
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);  
        }
    }

Bind the data in GridView,it is look like this

 //// Order by Descending
            GvStudent.DataSource = LinqToObject.GetStudentDataSortDescending(AddData());
            GvStudent.DataBind();

Step 5
Partitioning Operators

Take Method
Get the Top 3 Student data,it is look like this

 /// <summary>
    /// Get Top 3 Data by using Take method
    /// </summary>
    /// <param name="ListObj">Specify student List Object</param>
    /// <returns>List</returns>
    public static List<Student>  GetTop3StudentData(List<Student> ListObj)
    {
        try
        {
            var Query = (from Q in ListObj
                         select Q).Take<Student>(3);

            return Query.ToList<Student>();   
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);  
        }
    }

Bind the data to the GridView,it is look like this

///Top 3 Record
            GvStudent.DataSource = LinqToObject.GetTop3StudentData(AddData());
            GvStudent.DataBind(); 

The Take Method is used to decide how many records are to be fetched

Skip Method
Get all data but skip first 2 student data from student object,it is look like this
/// <summary>
    /// Get all student data but skip first 2 student data from Student object
    /// </summary>
    /// <param name="ListObj">Specify student List Object</param>
    /// <returns>List</returns>
    public static List<Student> Skip(List<Student> ListObj)
    {
        try
        {
            var Query = (from Q in ListObj
                         select Q).Skip<Student>(2);

            return Query.ToList<Student>(); 
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);  
        }
    }

Bind the data in Gridview,it is look like this

 ///Skip first 2 data and display remaining data
            GvStudent.DataSource = LinqToObject.Skip(AddData());
            GvStudent.DataBind(); 

4 comments:

  1. Hi Kishore..Add screen shot of Ouput, If possible; so that viewers can visualize things/logic in better way.

    ReplyDelete
  2. Sure,Thanks for given Suggestion...

    ReplyDelete
  3. Dude! your tutorials are quite interesting and meaningful.
    Please put some tuts on [Silverlight following MVVM pattern ]
    and also tricks and techniques to follow in MVVM pattern.
    thanx! n regards :
    parasgreens :))

    ReplyDelete