英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论