In this tutorial we will discuss how to work with LIST using LINQ. For this example i am going to use the user defined data i.e. class. Create class and add some variable in it. I created the class name TechAltum and add some variable in it which is as follows:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for TechAltum
/// </summary>
public class TechAltum
{
public int stu_id;
public string stu_name;
public string stu_course;
}
Now create list of type TechAltum data type and add some objects in it which is as follows and make query using LINQ which is as follow:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//creat list and add some object
List<TechAltum> data = new List<TechAltum>() {
new TechAltum{ stu_id=1, stu_name="Isha", stu_course="asp.net"},
new TechAltum{ stu_id=2, stu_name="avinash", stu_course="Web Designing"},
new TechAltum{ stu_id=3, stu_name="Rahul", stu_course="asp.net"},
new TechAltum{ stu_id=4, stu_name="meena", stu_course="oracle"}
};
//as we know linq return in IEnumerable so i carried data in IEnumerable of type TechAltum
IEnumerable<TechAltum> res = from final_Res in data
where final_Res.stu_id > 2 && final_Res.stu_course == "asp.net"
select final_Res;
foreach (TechAltum ta in res)
{
Response.Write("Student Id=" + ta.stu_id + " Student Name=" + ta.stu_name + " Student Course=" + ta.stu_course);
}
}
}
The output of this code is as follows:-