Monday, August 9, 2010

JOIN - Linq to Objects

We have two List<> methods namely GetStudents() which get us the information about the Students and GetFlagNames() which give us information about Flag.

public List GetStudents()
{
return new List
{
new Student{Name="Smilu",Address="EKM",PhNo="987654",flag=1},
new Student{Name="JK", Address="Parur",PhNo="897643",flag=0},
new Student{Name="Kiran", Address="EKM",PhNo="798654",flag=1}
};
}

public List GetFlagNames()
{
return new List
{
new FlagName{flagID=0,flagName="CS"},
new FlagName{flagID=1,flagName="Network"}
};
}
---------------------------------------------------
We store the list object into a variable called "mystudents" and "flags" respectively...
var mystudents = GetStudents();
var flags = GetFlagNames();

This query represents the Joining using LinQ
var query = from s in mystudents
join f in flags on s.flag equals f.flagID
select new { s.Name, s.PhNo, f.flagName };


No comments: