C# 使用LINQ进行连接、分组和聚合
我对LINQ有点陌生,我正在尝试将下面的SQL写入LINQ语句。 除了SQL版本中的聚合部分(C# 使用LINQ进行连接、分组和聚合,c#,linq,join,group-by,aggregate,C#,Linq,Join,Group By,Aggregate,我对LINQ有点陌生,我正在尝试将下面的SQL写入LINQ语句。 除了SQL版本中的聚合部分(COUNT(oec.CourseTitle))之外,它还能工作,我不知道如何在LINQ中实现这一点。感谢您的帮助。谢谢 SQL select oec.OnlineEducationCourseId, oec.CourseTitle, COUNT(oec.CourseTitle) as CourseCount from OnlineEducationRegistration as oer join O
COUNT(oec.CourseTitle)
)之外,它还能工作,我不知道如何在LINQ中实现这一点。感谢您的帮助。谢谢
SQL
select
oec.OnlineEducationCourseId,
oec.CourseTitle,
COUNT(oec.CourseTitle) as CourseCount
from OnlineEducationRegistration as oer
join OnlineEducationCourse oec on oec.OnlineEducationCourseId = oer.OnlineEducationCourseId
where oer.District='K20'and DateCompleted BETWEEN '2013-01-01' AND '2014-01-01'
group by oec.CourseTitle,oec.OnlineEducationCourseId;
LINQ
var r = (from oer in db.OnlineEducationRegistrations
join oec in db.OnlineEducationCourses on oer.OnlineEducationCourseId equals
oec.OnlineEducationCourseId
where oer.District == districtId &&
oer.DateCompleted >= start &&
oer.DateCompleted <= end
group new {oer, oec} by new {oec.CourseTitle, oec.OnlineEducationCourseId}).ToList();
foreach (var item in r)
{
var courseId = item.Key.OnlineEducationCourseId;
var courseTitle = item.Key.CourseTitle;
// aggregate count goes here
}
var r=(来自db.Online教育注册中的oer
在db.OnlineEducationCourseId equals上加入oec在线教育课程
在线教育课程
其中oer.District==districtId&&
oer.DateCompleted>=开始和结束
oer.DateCompleted基本上只是
var courseCount = item.Count();
或者,您可以将其添加到“选择”列表中
var r = (from oer in db.OnlineEducationRegistrations
join oec in db.OnlineEducationCourses on oer.OnlineEducationCourseId equals
oec.OnlineEducationCourseId
where oer.District == districtId &&
oer.DateCompleted >= start &&
oer.DateCompleted <= end
group new {oer, oec} by new {oec.CourseTitle, oec.OnlineEducationCourseId} into g
select new {g.OnlineEducationCourseId, g.CourseTitle, CourseCount = g.Count() }).ToList();
foreach (var item in r)
{
var courseId = item.OnlineEducationCourseId;
var courseTitle = item.CourseTitle;
var courseCount = item.CourseCount;
}
var r=(来自db.Online教育注册中的oer
在db.OnlineEducationCourseId equals上加入oec在线教育课程
在线教育课程
其中oer.District==districtId&&
oer.DateCompleted>=开始和结束
完成日期