How to Run Linq Query Against Array and List

By
Linq (Language Integrated Query):- It is a programming language feature through which we can link query within programming language itself.We can run link query against any data source .When we run Linq query then most of the error regarding query we get to know at compile time.

Var :-  This is a new data type which set the type of variable according to the value assign to the variable.  Means if we assign int type value in var variable then  its type will be integer,so we can not assign any other type instead of int type in var variable in whole program.
Ex.
var sname ="ABC';
Since we declare sname as var type and also assign string value("ABC") in this variable(sname) ,so its type is string,that means we can't  assign any other type value in sname variable in whole program.
Note:- You can assign any type (int,string,char,float ,etc) values in Var type variables.

Type Initialization :-
student obj =new student {id =101,sname ="ABC',sage =20};

Anominous Type :- Through this feature we can directly make the object of such type which is not declared.That is known as anominous type.
Ex
var res = new {id=22,sname="ram",sage =25}
Response.write(res.id.ToString());
Response.write(res.sname);
Response.write(res.sage.ToString());

Linq Query against Array and List :-
Now i am going to explain"How to run Linq query Against array and List". There are some steps to perform this task in visual studio as given below:-
Step 1:- First open your visual studio-->File -->New -->website -->Select ASP.NET Empty website-->OK -->open solution Explore -->Add new web form-->Drag and DropdownList and Button control on the form as shown below:-

WEBFORM

Step 2:- Now Double click on each show button --> and write the Linq query codes as given below:-
using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    public class student
    {
        public int id;
        public string sname;
        public int sage;
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string[] str = { "Ram","Shayam","Sachin", "Ajay","Neha" };
        var res = from s in str where s.StartsWith("S")select s;
        DropDownList1.DataSource=res;
        DropDownList1.DataBind();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        System.Collections.Generic.List<student> obj = new System.Collections.Generic.List<student>();
        obj.Add(new student { id = 101, sname = "Ram", sage = 22 });
        obj.Add(new student { id = 102, sname = "Kiran", sage = 25 });
        obj.Add(new student { id = 101, sname = "kanchan", sage = 25 });
        obj.Add(new student { id = 101, sname = "Neha", sage = 19 });
        var res = from s in obj where s.sage >= 20 select s.sname;
        DropDownList2.DataSource = res;
        DropDownList2.DataBind();
    }
}
Step 3:- Now Run the program press F5 --> press each show button -->You will see following output as shown below:-


Ling_output

Description :-
  • First drop down list is  showing only those name which First letter start with  "S".
  • Second drop down list is showing the name of those student which age >=20.
Note:- You can use this Linq Query concepts in your asp.net application easily.

For More...
  1. Read ,write,search.. software install on your windows
  2. Introduction of wpf concepts
  3. How to build a calculator application and install it on your machine.
  4. How to implement page life cycle concepts in asp.net
  5. How to build form filling application in asp.net like IBPS 
  6. How to install .mdf file in sql server easily
  7. How to connect your database through oleDB 
  8. How to use web services in asp.net application
  9. How to implement different connection string in asp.net
  10. How to implement caching concepts in asp.net
Download whole attached file from below:-
            Download

0 comments:

Post a Comment

Powered by Blogger.