EF其他方式为Sum()新列

时间:2014-12-05 19:26:02

标签: entity-framework

我卡在这个计算新列的查询上。 我无法简单解释,只需看下面的代码段。

from user in context.Table
select new 
{
    Total = user.Total,
    Paid = user.Paid,
    Balance = //should be Total - Paid to assign result
} 

我试过这个查询

var result = from a in context.EnrollmentRequests
                         where a.SchoolYear == SchoolYear
                         select new
                         {
                             a.StudentID,
                             Name = a.Student.FirstName + " " + a.Student.MiddleName + " " + a.Student.LastName,
                             Tuition = context.Miscs.Where(m => m.YearLevel == a.YearLevel && m.SchoolYear == SchoolYear && m.Term == a.Term && m.CourseID == a.CourseID)
                                                .Select(ms => new { Amount = ms.Amount })
                                                    .Union(context.StudentCharges
                                                        .Where(s => s.YearLevel == a.YearLevel && s.SchoolYear == SchoolYear && s.Term == a.Term && s.CourseID == a.CourseID && s.StudentID == a.StudentID)
                                                            .Select(ss => new { Amount = ss.Amount }))
                                                                .Union(context.StudentSubjectTakes
                                                                    .Where(st => st.StudentID == a.StudentID && st.SchoolYear == a.SchoolYear && st.Term == a.Term && st.YearLevel == a.YearLevel && st.EducationalLevel == a.Student.StudentAdvanceEducations.FirstOrDefault().EducationLevel)
                                                                        .Select(st => new
                                                                        {
                                                                            Amount = context.SubjectOfferedFees
                                                                                .Where(f => f.SubjectsOfferedID == st.SubjectsOfferedID).Sum(w => (decimal?)w.Cost ?? 0)
                                                                        }))
                                                                                .Select(f => f.Amount).Sum(),
                             PaymentMade = context.Payments.Where(p => p.SchoolYear == SchoolYear && p.Term == a.Term && p.StudentID == a.StudentID && p.PaymentDes == "Tuition Fee").Sum(sm => (decimal?)sm.Amount),
                             Balance = Tuition - PaymentMade //Does not exist on current context
                         };

但不起作用它表示在当前上下文中不存在。

怎么可能这样呢。 谢谢。这对任何人都有帮助。

1 个答案:

答案 0 :(得分:1)

Balance = user.Total - user.Paid