LINQ-按年/月分组

时间:2019-09-19 11:00:48

标签: c# linq charts group-by

我试图在最近四个月内用总数(总和)填充图形数据,并且看起来像这样:

enter image description here

到目前为止,我已经尝试按年份和月份对数据进行分组,但是我不确定这是否正确,因为这行不通。

代码如下:

 var testQUERY = await _context.Calculation
             .AsNoTracking()
             .Where(x => (x.PaymentDate != null && x.PaymentDate > DateTime.UtcNow.AddMonths(-4)))
            .GroupBy(x => new { x.PaymentDate.Value.Year, x.PaymentDate.Value.Month}).ToListAsync();

这是我的付款日期: enter image description here

我想知道如何仅按月分组。 我面临的错误是下一个:

  

生成警告错误   'Microsoft.EntityFrameworkCore.Query.QueryClientEvaluationWarning:   LINQ表达式'GroupBy(new <> f__AnonymousType0`2(Year =   Convert([p] .PaymentDate,DateTime).Year,Month =   Convert([p] .PaymentDate,DateTime).Month),[p])'不能为   翻译,并将在本地进行评估。”。此异常可以是   通过传递事件ID禁止显示或记录   'RelationalEventId.QueryClientEvaluationWarning'到   'DbContext.OnConfiguring'中的'ConfigureWarnings'方法或   “ AddDbContext”。

P.S如果我因为使用​​

而更好地考虑
x.PaymentDate != null && x.PaymentDate > DateTime.UtcNow.AddMonths(-4)

我也不需要在其中包含Year的新匿名类型。但是,我试图按不存在的列进行分组。

1 个答案:

答案 0 :(得分:1)

尝试使用这个。请参阅评论以获取可能的修复程序。

private boolean focused; //a field
protected class FocusListenerColorLine implements FocusListener {

    @Override
    public void focusGained(FocusEvent e) {
        firePropertyChange(PROPERTY_LINE_COLOR, colorLineInactive, colorLineActive);
        focused = true;
    }

    @Override
    public void focusLost(FocusEvent e) {
        firePropertyChange(PROPERTY_LINE_COLOR, colorLineActive, colorLineInactive);
        focused = false;
    }
}



@Override
public void paintSafely(Graphics g) {
    super.paintSafely(g);
    paintLine(g);
    changeColorOnFocus(g);
}

protected void changeColorOnFocus(Graphics g) {
    boolean hasFocus = focused;
    JTextComponent c = getComponent();
    if (c == null) {
        return;
    }
    if (hasFocus && (activeBackground != null) && (activeForeground != null)) {
        logicForChangeColorOnFocus(c, activeBackground, activeForeground);
        //TODO create a new changePropriety
        paintLine(c.getGraphics());
    }

    if (!hasFocus && (inactiveBackground != null) && (inactiveForeground != null)) {
        logicForChangeColorOnFocus(c, inactiveBackground, inactiveForeground);
        paintLine(c.getGraphics());
    }
}
相关问题