在ASP.NET Core中使用Entity Framework进行多个SUM查询,可以按照以下步骤进行:
public class ApplicationDbContext : DbContext
{
public DbSet Products { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("YourConnectionString");
}
}
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
public class ProductController : Controller
{
private readonly ApplicationDbContext _context;
public ProductController(ApplicationDbContext context)
{
_context = context;
}
public IActionResult GetProductSummary()
{
var summary = _context.Products
.GroupBy(p => 1)
.Select(g => new
{
TotalPrice = g.Sum(p => p.Price),
AveragePrice = g.Average(p => p.Price)
})
.FirstOrDefault();
return Json(summary);
}
}
在上面的示例中,我们先对产品进行分组,然后使用Sum和Average方法计算总价格和平均价格。最后,我们使用FirstOrDefault方法获取结果。
请注意,上述示例中的"YourConnectionString"应替换为你自己的数据库连接字符串。
这是一个简单的示例,你可以根据自己的需求进行修改和调整。希望对你有帮助!