- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我一直在查看此处的其他线程以了解如何在 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
};
pending
是 List<T>
除其他列外,它还包含 ContactID
和 Amount
,但对于此查询,我只关心这两个。
问题是,在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/
我有一个序列 IGrouping IEnumerable> sequence = ...; 和谓词 Func predicate = ...; 如何过滤Element s 通过将结果保持为 IEnum
我有一个序列 IGrouping IEnumerable> sequence = ...; 和谓词 Func predicate = ...; 如何过滤Element s 通过将结果保持为 IEnum
尝试使用 Automapper 对按两个属性分组的分组查询进行投影时出现以下错误 Missing map from IGrouping`2 to PlayerWarScore. Create usin
我想使用 Linq 创建一个函数来汇总传入的值序列。该函数应如下所示: IDictionary> Summarize(IEnumerable values) { return values
我有一个 LINQ 问题,这让我有点困惑。我可以看到很多替代方法来找到解决方案,但我想尝试解决问题,因为它困扰着我! public enum AnimalType { Cat, Dog
我有一个 IGrouping 类型的对象,想在不更改对象类型的情况下对组内的元素进行排序。 换句话说,我有 var tmp = group.OrderBy(x => x); 与 group类型 IGr
大家好。 假设我有以下类(class): public class ObjectA { public string PropertyA { get; set; }
我有一些代码可以创建列表列表。该列表是这种类型的: List> GroupedDeviceList = new List>(); 但需要返回如下类型的结果: IEnumerable> 这是否可以通
我一直在查看此处的其他线程以了解如何在 linq 中执行 GroupBy。我正在遵循适用于其他人的 EXACT 语法,但它不起作用。 这是查询: var results = from p in pen
我正在尝试创建一个 WCF 数据服务 ServiceOperation,它在服务器端进行分组,然后将数据发送到客户端。 当我尝试调用它(甚至连接到该服务)时,出现错误。它说它不能构造接口(interf
在 IEnumerable包含定义为: class Appointment { public string Name { get; set; } public DateTime LastMod
我目前有: UIEnumerable > 从这段代码: var queryDupFiles = from file in fileList group file by new Portable
IEnumerable> datas = list.GroupBy(x => x.PropertyXYOfMyClass); // get all items from each group fore
我目前有: UIEnumerable > 从这段代码: var queryDupFiles = from file in fileList group file by new Portable
IEnumerable> datas = list.GroupBy(x => x.PropertyXYOfMyClass); // get all items from each group fore
我有两个类: class Foo { public Bar SomeBar { get; set; } } class Bar { public string Name { get;
IGrouping 支持 ElementAt 方法来索引分组的集合。那么为什么方括号运算符不起作用呢? 我可以做类似的事情 list.GroupBy(expr).Select(group => gr
如何直接从 IGrouping 中删除对象 IGrouping ? 目前我所知道的唯一方法是生成一个没有相关元素的新 IGrouping,但我不喜欢这种方式,因为它会在我的应用程序中引起一些麻烦。 有
我已经在列表上应用了 IGrouping<> - 它看起来像这样: IEnumerable> Tiers { get { return ActiveNodes.GroupBy(x => new
这个问题在这里已经有了答案: ILookup vs. IGrouping (3 个答案) 关闭 9 年前。 ILookup 和 IGrouping 是非常相似的 Linq 接口(interface)
我是一名优秀的程序员,十分优秀!