Sunday 2 February 2014

What is Linq to Objects (LINQ to Lists)

What is Linq to Objects:
 
Linq to objects refer to the use of Linq queries with any IEnumerable or IEnumerable<T> collection directly ,without the use of any intermediate Linq provider or interface

Linq to Objects is more beneficial because of these specific reasons
  1.  They are more concise and readable especially when it is used to filter multiple conditions 
  2.  They  provide powerful filtering, ordering  and grouping capabilities with the use of minimum application code
  3. They can be easily ported to other data sources with little or no modification

LINQ to Lists: In this example we will discuss LINQ queries on Lists, the advantage of linq is we no need to execute/initialize the linq query every time, initialize once and use where ever required. The below example will explain how it will work. 

LINQ Query: Below LINQ query will find the Student  which are having Fee 5000  between 80000 from Student list.

Code for  Default.aspx………………………………
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<center> <asp:Button ID="btngetdata"  runat="server"  Text="Linq to Objects"
onclick="btngetdata_Click" /></center>
<center> <asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</center>
</div>
</form>
</body>
</html>

Code for  Default.aspx.cs………………………………
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
class Student
{
private int IntRollNo;

public int RollNo
{
get { return IntRollNo; }
set { IntRollNo = value; }
}
private string StrName;

public string Name
{
get { return StrName; }
set { StrName = value; }
}
private string StrCourse;

public string Course
{
get { return StrCourse; }
set { StrCourse = value; }
}
private decimal DecFee;

public decimal Fee
{
get { return DecFee; }
set { DecFee = value; }
}
}
//create instance of List..............

List<Student> stulist = new List<Student>();

protected void Page_Load(object sender, EventArgs e)
{

}
protected void btngetdata_Click(object sender, EventArgs e)
{
stulist.Add(new Student { RollNo = 1, Name = "Udayan", Course = "Mca", Fee = 30000 });
stulist.Add(new Student { RollNo = 2, Name = "Kush", Course = "Mca", Fee = 40000 });
stulist.Add(new Student { RollNo = 3, Name = "Naved", Course = "Bca", Fee = 60000 });
stulist.Add(new Student { RollNo = 4, Name = "Ramu", Course = "Mca", Fee = 70000 });
stulist.Add(new Student { RollNo = 5, Name = "Suresh", Course = "B.A.", Fee = 3000 });

var studentlist = from s in stulist where s.Fee >5000 && s.Fee < 80000 orderby s.RollNo select new { RollNo = s.RollNo, Name = s.Name, Course = s.Course, Fee = s.Fee };

GridView1.DataSource = studentlist;
GridView1.DataBind();
}
}



1 comments:

  1. I wish to do crud operation in grid with linq to object . Is it possible Kush Sir?

    Please explain this...

    ReplyDelete