Entity Framework Sum查询可等待

huangapple go评论99阅读模式
英文:

Entity Framework Sum Query Awaitable

问题

我正在尝试将一个求和语句变为可等待的:

public class MyClassOne
{
    public int id { get; set; }
    public decimal Amount { get; set; }
}

public class MyClassTwo
{
    public decimal TotalAmount { get; set; }
}

MyClassTwo myClassTwo = new MyClassTwo();

myClassTwo.TotalAmount = await _context.MyClassOne
                                      .Where(p => p.id == 1)
                                      .SumAsync(x => x.Amount);

这段代码按预期工作,不过现在是可等待的,我无法弄清如何修改它以实现这一点。

谢谢。

英文:

I'm trying to make a sum statement awaitable:

public class MyClassOne
{
    public int id { get; set; }
    public decimal Amount { get; set; }
}

public class MyClassTwo
{
    public decimal TotalAmount { get; set; }
}

MyClassTwo myClassTwo = new MyClassTwo();

myClassTwo.TotalAmount = _context.MyClassOne
                                 .Where(p => p.id == 1.Sum(x => x.Amount);

This works as expected, except it's not awaitable and I can't figure out how to modify it to make it so.

Thanks

答案1

得分: 2

使用SumAsync(EF)SumAsync(EF Core),根据您使用的版本:

myClassTwo.TotalAmount = await _context.MyClassOne
    .Where(p => p.id == 1)
    .SumAsync(x => x.Amount);
英文:

Use SumAsync (EF) or SumAsync (EF Core), depending on the version you're using:

myClassTwo.TotalAmount = await _context.MyClassOne
    .Where(p => p.id == 1)
    .SumAsync(x => x.Amount);

huangapple
  • 本文由 发表于 2023年8月5日 08:53:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76839749.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定