Introduction
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:-
TechAltum.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public class TechAltum
{
public int stu_id;
public string stu_name;
public string stu_course;
}
For Example
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<viastudy> data = new List<viastudy>() {
new viastudy{ stu_id=1, stu_name="durgesh", stu_course="asp.net"},
new viastudy{ stu_id=2, stu_name="shallu", stu_course="Web Designing"},
new viastudy{ stu_id=3, stu_name="duggu", stu_course="asp.net"},
new viastudy{ stu_id=4, stu_name="bittu", stu_course="oracle"}
};
//as we know linq return in IEnumerable so i carried data in IEnumerable of type viastudy
IEnumerable<viastudy> res = from final_Res in data
where final_Res.stu_id > 2 && final_Res.stu_course == "asp.net"
select final_Res;
foreach (viastudy 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:-
No comments:
Post a Comment