[英]IGrouping does not contain a definition for
I've been looking at other threads here to learn how to do a GroupBy in linq. 我一直在这里查看其他线程,以了解如何在linq中执行GroupBy。 I am following the EXACT syntax that has worked for others, but, it's not working. 我正在使用对其他人有用的EXACT语法,但是,它不起作用。
Here's the query: 这是查询:
var results = from p in pending
group p by p.ContactID into g
let amount = g.Sum(s => s.Amount)
select new PaymentItemModel
{
ContactID = g.ContactID, // <--- Error here
Amount = amount
};
pending
is a List<T>
that contains, among other columns, ContactID
and Amount
, but those are the only two I care about for this query. pending
是一个List<T>
,其中除其他列外还包含ContactID
和Amount
,但这是我对此查询仅关心的两个。
The trouble is, inside the the select new
, the g.
麻烦的是,在select new
的g.
won't give me any of the columns inside the original list, pending
. 不会给我原始列表内的任何列, pending
。 And when I try, I get: 当我尝试时,我得到:
IGrouping
<int, LeadPurchases>
does not contain a definition for ContactID, and no extension method blah blah blah... IGrouping<int, LeadPurchases>
不包含ContactID的定义,也没有扩展方法等等……
This is the SQL I am trying to emulate: 这是我要模拟的SQL:
SELECT
lp.PurchasedFromContactID,
SUM (lp.Amount)
FROM
LeadPurchases lp
GROUP BY
lp.PurchasedFromContactID
You are grouping on the basis of ContactID
, so it should be the Key for the result, So you have to use g.Key
instead of g.ContactID
; 您是根据ContactID
进行分组的,因此它应该是结果的键,因此您必须使用g.Key
而不是g.ContactID
; Which means the query should be like the following: 这意味着查询应类似于以下内容:
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
};
updates : 更新 :
If you want to perform grouping based on more than one column then the GroupBy clause will be like this: 如果要基于多个列进行分组,则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
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.