use*_*932 3 .net c# linq sql-server
任何人都可以帮助我"如何选择LINQ中不在Group By子句中的字段"
var ReportData =
from a in context.Table1
from b in context.Table2
where a.personelID == b.ID
&& a.DateField.Value.Year == 2011
group a by new { a.personelID, b.Name, b.Surname} into grouping
select new
{
// grouping.Key.DateField,
Name= grouping.Key.Name,
Surname= grouping.Key.Surname,
PagaBruto = grouping.Sum(i => i.Bruto)),
};
Run Code Online (Sandbox Code Playgroud)
我不能选择字段"DateField"是在表1,我不希望在这个领域Group By,因为我会得到一个错误的结果.谢谢!
我认为您需要在分组之前选择两个表成员,以便从两者中选择数据.我做了一个连接,而不是你的where子句.
(from a in context.Table1
join b in context.Table2 on a.PersonalID equals b.ID
select new { A=a, B=b } into joined
group joined by new { joined.A.PersonalID, joined.B.Name, joined.B.Surname } into grouped
select grouped.Select(g => new {
DateField= g.A.Datefield,
Name = g.B.Name,
Surname = g.B.Surname,
PagaBruto = g.A.Sum(i => i.Bruto)
})).SelectMany(g => g);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4880 次 |
| 最近记录: |