How to Run Linq query against DataSet in.NET

By
Hi Friends! Today i will show you,"How to Run Linq query against DataSet ".In this ,i will filter the data from database with the help of Data set.This is very simple concepts but this more important for small and  large projects.You can use this concepts on your projects if it required.Data set is basically a cache memory in .NET Which i have already explained in Ado.net section.
There are following steps to implement this concepts in .net application as given below:-
Step 1 :- First open your visual studio -->File -->New -->website -->Select ASP.NET Empty website -->OK -->Open Solution Explorer -->Add New Web Form (Default.aspx) -->Drag and Drop List Box and Button controls on the form as shown below:-

linq_output
 Step 2 :- Now Open your sql server Database --> Create a table (student) with 3 column and insert some values as shown below:-


linq_out
Step 3:- Now Write the c# codes in Default.aspx.cs file as given below:-
using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data source = RAMASHANKER-PC;integrated security = yes;initial catalog =master");
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter("select *from student",con);
        DataSet ds = new DataSet();
        da.Fill(ds);       
            var res = from st in ds.Tables[0].AsEnumerable()
                      where st.Field<int>("sid") == 106
                      select st.Field<string>("name");
            ListBox1.DataSource = res;
            ListBox1.DataBind();
    }
}

Step 4:- Now Run the application (press F5) -->Press Submit button -->You will see the following output as shown below:-

linq_out

I hope this is helpful for you.
Download Attached file
  Download

0 comments:

Post a Comment

Powered by Blogger.