var result = from ca in context.candidates
join i in context.INSTRUCTOR_COURSE_SESSIONs on ca.emplid equals i.emplid
group i by i.current_year into groupKey
select new { Max = groupKey.Key,
Fields = groupKey
};
foreach (var r in result){
Console.WriteLine(r.Max);
foreach (var a in r.Fields)
Console.WriteLine(a.emplid);
}
I hope this help.
Linq
Kindly mark this post as "Answer", if it helped you.
select max(i.current_year), i.course,
ca.emplid from candidate ca
join INSTRUCTOR_COURSE_SESSION i
on ca.emplid = i.emplid groupby i.course, ca.emplid
and the relevant linq query is
from ca in candidate
join INSTRUCTOR_COURSE_SESSION i on ca.emplid equals i.emplid
group ca by new { ca.emplid, i.course } into g
select new
{
Course = g.Key.course,
Emplid = g.Key.emplid,
MaxYear = g.Max(t => t.current_year)
}
This should be the solution, i dont have visual studio here, so im not sure about the syntax errors.
@pierrrefrc ok, so as we are using the current_year column from
INSTRUCTOR_COURSE_SESSION i so we need to group by i. ok got it. thanks for the correction.
DivakarGanta
Member
39 Points
140 Posts
convert SQL to LINQ Groupby.
May 15, 2012 07:03 AM|LINK
Pls, giveme the solution.
Linq
pierrefrc
Participant
947 Points
201 Posts
Re: convert SQL to LINQ Groupby.
May 15, 2012 11:19 AM|LINK
Hi
Try:
var result = from ca in context.candidates join i in context.INSTRUCTOR_COURSE_SESSIONs on ca.emplid equals i.emplid group i by i.current_year into groupKey select new { Max = groupKey.Key, Fields = groupKey }; foreach (var r in result){ Console.WriteLine(r.Max); foreach (var a in r.Fields) Console.WriteLine(a.emplid); }I hope this help.
Linq
sriharsha241...
Member
414 Points
109 Posts
Re: convert SQL to LINQ Groupby.
May 15, 2012 11:20 AM|LINK
Hi dude,
I suppose the query should be as
select max(i.current_year), i.course, ca.emplid
from candidate ca
join INSTRUCTOR_COURSE_SESSION i
on ca.emplid = i.emplid
group by i.course, ca.emplid
and the relevant linq query is
from ca in candidate
join INSTRUCTOR_COURSE_SESSION i on ca.emplid equals i.emplid
group ca by new { ca.emplid, i.course } into g
select new
{
Course = g.Key.course,
Emplid = g.Key.emplid,
MaxYear = g.Max(t => t.current_year)
}
This should be the solution, i dont have visual studio here, so im not sure about the syntax errors.
pierrefrc
Participant
947 Points
201 Posts
Re: convert SQL to LINQ Groupby.
May 15, 2012 11:29 AM|LINK
I think that need a correction.
to
group i by new { ca.emplid, i.course } into g
sriharsha241...
Member
414 Points
109 Posts
Re: convert SQL to LINQ Groupby.
May 15, 2012 12:20 PM|LINK
@pierrrefrc ok, so as we are using the current_year column from INSTRUCTOR_COURSE_SESSION i so we need to group by i. ok got it. thanks for the correction.