实体框架核心:使用计算字段进行分组

问题描述

我正在尝试编写查询。我到目前为止所拥有的如下所示。

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

有没有办法做到这一点?我似乎无法select之前group by自己的一组列。如何将计算所得的字段插入每个项目?

注意:StorageState是一个枚举。在所有这些表达式中,我都可以轻松地将其转换为int。我可以弄清楚那部分,但是弄清楚这和我的问题是分开的。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)