2014-12-05 69 views
0

我被困在這個計算新列的查詢上。 我不能簡單解釋一下,只能看到下面的代碼片段代碼。EF其他方式Sum()新列

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

餘額= user.Total - user.Paid

+0

謝謝您的回答,但問題是iv'e分配給Total和Paid列的另一個查詢。 Like Total = context.Table2.Where(f => f.Id).Select(s => s.Amount).Sum() Paid = context.Table3.Where(f => f.Id)。選擇(S => s.PaymentAmount).Sum() 在我目前我得到了很長的lambda查詢,所以即時尋找最短的路.. ..你的答案仍然正確.. – mardagz 2014-12-05 19:36:36

+0

編輯我的問題。 – mardagz 2014-12-05 19:41:01