gpt4 book ai didi

c# - IGrouping 不包含定义

转载 作者:太空宇宙 更新时间:2023-11-03 19:49:15 24 4
gpt4 key购买 nike

我一直在查看此处的其他线程以了解如何在 linq 中执行 GroupBy。我正在遵循适用于其他人的 EXACT 语法,但它不起作用。

这是查询:

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
};

pendingList<T>除其他列外,它还包含 ContactIDAmount ,但对于此查询,我只关心这两个。

问题是,在select new里面, g.不会给我原始列表中的任何列,pending .当我尝试时,我得到:

IGrouping <int, LeadPurchases> does not contain a definition for ContactID, and no extension method blah blah blah...

这是我要模拟的 SQL:

SELECT 
lp.PurchasedFromContactID,
SUM (lp.Amount)
FROM
LeadPurchases lp
GROUP BY
lp.PurchasedFromContactID

最佳答案

你是根据ContactID分组的,所以它应该是结果的Key,所以你必须使用g.Key而不是g .联系方式;这意味着查询应该如下所示:

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
};

关于c# - IGrouping 不包含定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41733775/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com