【发布时间】:2020-08-14 00:49:49
【问题描述】:
我正在尝试编写查询。我目前所拥有的如下所示。
var x = from d in DbContext.StorageDetails
let state = (d.ReleaseDate.HasValue && d.ReleaseDate < date) || (d.TakeOrPayEndDate.HasValue && d.TakeOrPayEndDate < date) ?
StorageState.Closed :
(d.RailcarNumber == null || d.RailcarNumber == string.Empty) ?
d.TakeOrPayStartDate.HasValue ? StorageState.TakeOrPay : StorageState.Open :
(d.ArrivalDate.HasValue && d.ArrivalDate <= date) ? StorageState.Filled : StorageState.EnRoute
group d by new
{
d.CustomerId,
d.Customer.Name,
d.LocationId,
d.Location.City,
d.Location.State
} into g
select new
{
// ...
};
给我带来麻烦的部分是我想在每个项目中包含计算出的state 值。我不想按此值进行分组,但我希望能够对其求和。
// Note: This is after my group statement
select new
{
// state is a let variable and not part of x!
TotalOpen = g.Sum(x => x.state == StorageState.Open),
TotalClosed = g.Sum(x => x.state == StorageState.Closed),
// Etc.
};
有没有办法做到这一点?在group by 之前,我似乎无法select 我自己的一组列。如何将此计算字段插入到每个项目中?
注意:StorageState 是一个枚举。我可以在所有这些表达式中轻松地将其转换为int。我可以弄清楚那部分,但弄清楚它与我在这里的问题是分开的。
【问题讨论】:
-
评论不用于扩展讨论;这个对话是moved to chat。
标签: c# entity-framework asp.net-core .net-core entity-framework-core