您基于进行分组ContactID
,因此它应该是结果的键,因此您必须使用g.Key
而不是g.ContactID
; 这意味着查询应类似于以下内容:
from p in pending group p by p.ContactID into g let amount = g.Sum(s => s.Amount) select new PaymentItemModel { ContactID = g.Key, Amount = amount };
更新 :
如果要基于多个列进行分组,则GroupBy子句将如下所示:
group p by new { p.ContactID, p.Field2, p.Field3 }into g select new PaymentItemModel() { ContactID = g.Key.ContactID, anotherField = g.Key.Field2, nextField = g.Key.Field3 };