我必须在lambda表达式中转换给定的linq查询。即

var scholars = (from scholar in db.Scholars
                join suspension in db.Suspensions
                    on scholar.ID equals suspension.ScholarID

                where suspension.StartDate >= startDate &&
                      suspension.EndDate <= endDate

                group scholar by new { scholar.ID, scholar.FirstName, scholar.LastName }
                    into g

                select new
                {
                    FullName = g.Key.FirstName +" " + g.Key.LastName,
                    TotalSuspensionSum = g.Sum(x => x.Suspensions.Sum(y => y.SuspensionDays))
                })
                .ToList()
                .OrderBy(x=> x.FullName);

最佳答案

这是你的lambda:

var scholars = db.Scholars.Join(db.Suspensions,
        scholar => scholar.ID,
        suspension => suspension.ScholarID,
        (scholar, suspension) => new {scholar, suspension})
    .Where(u => u.suspension.StartDate >= startDate &&
                u.suspension.EndDate <= endDate)
    .GroupBy(u => new { u.scholar.ID, u.scholar.FirstName, u.scholar.LastName })
    .Select(u => new
        {
            FullName = u.Key.FirstName + " " + u.Key.LastName,
            TotalSuspensionSum = u.Sum(x =>
                x.scholar.Suspensions.Sum(y => y.SuspensionDays)
            )
        })
    .OrderBy(x => x.FullName)
    .ToList();

10-07 20:15