汇总ObservableCollection的总数<;我的类型>;带有Linq表达式
本文关键字:gt 类型 带有 表达式 Linq 我的 lt ObservableCollection 汇总 | 更新日期: 2023-09-27 18:28:35
我有一个Silverlight委托,它在EventArgs Result中获得一个ObservableCollection。DTO myType包含字段Order、StartDate、Status、PlannedAmount、ActualAmount和其他一些字段。WCF服务端的查询为每个订单获取几行,仅根据PlannedAmount和ActualAmount而变化。
Order # | StartDate | PlannedAmount| ActualAmount | Order Comments ....
Order A | March 15 | 20.00 | 0.00 | Comment 1 ...
Order A | March 15 | 30.00 | 0.00 | Comment 1 ...
Order A | March 15 | 10.00 | 0.00 | Comment 1 ...
Order A | March 15 | 0.00 | 30.00 | Comment 1 ...
Order B | March 25 | 10.00 | 0 | Comment 2 ...
Order B | March 25 | 0.00 | 5.00 | Comment 2 ...
我希望每个订单只显示一行,并将所有PlannedAmount和ActualAmount值相加。我更喜欢在表示层中更改这一点,因为我不知道WCF操作的其他使用者需要什么。所以我想把它汇总到…
Order # | StartDate | PlannedAmount| ActualAmount | Order Comments ....
Order A | March 15 | 60.00 | 30.00 | Comment 1 ...
Order B | March 25 | 10.00 | 5.00 | Comment 2 ...
然后把它做成一个ObservableCollection,和我以前的类型一样。
我已经尝试过了,但除了Key和Sum值之外,我似乎什么都得不到。
var Orders =
from wo in OrdersPerOperation
group wo by wo.OrderNo
into g
select new
{
OrderNo = g.Key,
Planned = g.Sum(wo => wo.Planned),
Actual = g.Sum(wo => wo.Actual),
OrderComments = g.Select(wo => wo.Equipment),
StartDate = g.Select(wo => wo.StartDate),
Status = g.Select(wo => wo.Status),
OrderType = g.Select(wo => wo.OrderType) //,...
};
编辑
只得到密钥和两个总和是直接的:
var Orders =
from wo in OrdersPerOperation
group wo by wo.OrderNo
into g
select new
{
OrderNo = g.Key,
Planned = g.Sum(wo => wo.Planned),
Actual = g.Sum(wo => wo.Actual)
}
挑战在于获得所有其他领域,这些领域是重复的,也会在结果中显示出来。
编辑
我猜测这可能像自引用SQL查询一样工作。我相信我在这方面走在了正确的轨道上,因为每个元素都有正确格式的数据。在所有这些Linq混乱之前,我仍然无法将结果设置为Silverlight网格的ItemsSource,我可以绑定它。IDE警告我FirstOrDefault可能是NullReferenceException。
var workOrders = from wo in workOrdersPerOperation
group wo by wo.OrderNo
into g
select new
{
OrderNo = g.Key,
Planned = g.Sum(wo => wo.Planned),
Actual = g.Sum(wo => wo.Actual),
g.FirstOrDefault(wo => wo.OrderNo == g.Key).Location,
g.FirstOrDefault(wo => wo.OrderNo == g.Key).Equipment,
g.FirstOrDefault(wo => wo.OrderNo == g.Key).StartDate,
g.FirstOrDefault(wo => wo.OrderNo == g.Key).Status,
g.FirstOrDefault(wo => wo.OrderNo == g.Key).OrderType,
g.FirstOrDefault(wo => wo.OrderNo == g.Key).AccType,
g.FirstOrDefault(wo => wo.OrderNo == g.Key).WorkCenter,
g.FirstOrDefault(wo => wo.OrderNo == g.Key).Description,
g.FirstOrDefault(wo => wo.OrderNo == g.Key).Priority
};
有人能帮我走过这一步吗?我仍然需要将它绑定到控件。
您需要g.SelectMany(wo => wo.Equipment)
。
我很确定这个
Location = g.FirstOrDefault(wo => wo.OrderNo == g.Key).Location,
将产生与此相同的结果
Location = g.FirstOrDefault().Location,
此外,如果集合可能为空,则只需要使用FirstOrDefault
。但是,如果组中没有项目,则该组将不存在。这允许您将其简化为以下内容。
Location = g.First().Location,
你可以制作的最终解决方案
var orders = from wo in ordersPerOperation
group wo by wo.OrderNo
into g
select new MyType
{
OrderNo = g.Key,
Planned = g.Sum(wo => wo.Planned),
Actual = g.Sum(wo => wo.Actual),
Location = g.First().Location,
Equipment = g.First().Equipment,
StartDate = g.First().StartDate,
Status = g.First().Status,
OrderType = g.First().OrderType,
AccType = g.First().AccType,
WorkCenter = g.First().WorkCenter,
Description = g.First().Description,
Priority = g.First().Priority
};
尝试:
OrderComments = g.Select(wo => wo.Equipment).ToList()
我发现我可以用初始化器选择MyClass的一组新实例。
var orders = from wo in ordersPerOperation
group wo by wo.OrderNo
into g
select new MyType()
{
OrderNo = g.Key,
Planned = g.Sum(wo => wo.Planned),
Actual = g.Sum(wo => wo.Actual),
Location = g.FirstOrDefault(wo => wo.OrderNo == g.Key).Location,
Equipment = g.FirstOrDefault(wo => wo.OrderNo == g.Key).Equipment,
StartDate = g.FirstOrDefault(wo => wo.OrderNo == g.Key).StartDate,
Status = g.FirstOrDefault(wo => wo.OrderNo == g.Key).Status,
OrderType = g.FirstOrDefault(wo => wo.OrderNo == g.Key).OrderType,
AccType = g.FirstOrDefault(wo => wo.OrderNo == g.Key).AccType,
WorkCenter = g.FirstOrDefault(wo => wo.OrderNo == g.Key).WorkCenter,
Description = g.FirstOrDefault(wo => wo.OrderNo == g.Key).Description,
Priority = g.FirstOrDefault(wo => wo.OrderNo == g.Key).Priority
};
这不是一个ObservableCollection,但事实证明我根本不需要它。