我正在尝试分组,然后旋转一些数据以显示在asp.net视图上。
这是我的View Model类。
public class myModel
{
public string id { get; set; }
public string fund{ get; set; }
public string account{ get; set; }
public string amount{ get; set; }
}
以下是从数据库返回的示例数据
id fund account amt
1 101 1001 25.70
2 101 1001 10.00
3 101 1001 12.00
4 101 1002 -5.0
5 201 2001 12.00
6 201 2001 11.00
现在我有一个查询返回上面的数据,并将其映射到上面提到的模型。从本质上讲,我有一个包含上述数据的对象列表。
现在,我想对这些数据进行分组和求和,并在我的视图中显示如下所示。
fund
account sum of all amt
account sum of all amt
fund
account sum of all amt
account sum of all amt
所以就像这样
101
1001 47.00
1002 -5
....
201
2001 23
and so on
我该怎么做呢?
我有一个上面提到的对象列表。
我正在考虑创建另一个类,并将其映射到它,但我可能会让它变得比需要的更复杂
public class pivot
{
public string fund { get; set; }
public List < pivotdetail >detail{ get; set; }
}
public class pivotdetail
{
pretty obvious
}
我该怎么做或者应该怎么做,有什么想法吗?
发布于 2019-06-06 06:03:52
您正在创建一个分组的分组。首先,在最深的级别分组,然后将这些组分组到更高的级别,以获得您想要的结构。
from x in Data
group x.Amount by new { x.Fund, x.Account } into g
group new
{
g.Key.Account,
Amount = g.Sum(),
} by g.Key.Fund
请注意,这通常效率较低,因为它将为每个组创建新的查询。您可能希望将其限制为数据库上的单个分组调用,并在代码中进一步分组。
from g in (from x in Data
group x.Amount by new { x.Fund, x.Account } into g
select new
{
g.Key.Fund,
g.Key.Account,
Amount = g.Sum()
}).AsEnumerable()
group new
{
g.Account,
g.Amount,
} by g.Fund;
https://stackoverflow.com/questions/56468188
复制相似问题