Can someone help me converting a SQL Query into LINQ expression?
Can someone help me converting the following SQL Query into LINQ expression?
select student.StudentID,
student.FirstName + ' ' + student.MiddleInitial + ' ' + student.LastName,
student.OpenDate,
acct.Balance
from Student student
inner join
(Select StudentID, Sum(Amount) as Balance
from Account
Group by StudentID) as acct on student.StudentID = acct.StudentID
The following LINQ expression works! Thank you so much Phil!!!
var results = (from s in Students
join a in Accounts on s.StudentID equals a.StudentID
group a by new
{s.StudentID, s.FirstName, s.MiddleInitial, s.LastName, s.OpenDate}
into z
select new 开发者_StackOverflow中文版
{ StudentID = z.Key.StudentID,
FirstName = z.Key.FirstName,
MiddleInitial = z.Key.MiddleInitial,
LastName = z.Key.LastName,
OpenDate = z.Key.OpenDate,
Balance = z.Sum(a=> a.Amount)
}
);
Thank you!
Give this a try?
var stu =
from s in db.Students
join a in db.Account on s.StudentID equals a.StudentID
group a by new
{s.StudentID, s.FirstName, s.MiddleInitial, s.LastName, s.OpenDate}
into z
select new
{ StudentID = z.Key.StudentID,
FullName = string.Format("{0} {1} {2}",z.Key.FirstName, z.Key.MiddleInitial, z.Key.LastName),
OpenDate = z.Key.OpenDate,
Balance = z.Sum(a=>a.Amount)
};
精彩评论