[英]How to make an Entity Framework query with a sum of the price that depends of the date?
Hello StackOverflow community,你好 StackOverflow 社区,
I have a question,我有个问题,
I want to create a query which is able to do the some of all price (which is a double), depends of the date.我想创建一个查询,它能够执行所有价格中的一些(这是双倍的),具体取决于日期。
--SQL Query
select sum(price) from dbo.contracts where create_date ='2009-01-01' group by (id);
I tried this Linq query but it didn't worked:我试过这个 Linq 查询,但没有奏效:
--Linq Query
public static double getPriceFromMonth(int year, int month)
{
return unitOfWork.Contracts.GetAll().Where(c => c.create_date.Year == year && c.create_date.Month == month).GroupBy(c => c.id).Select(s => new { total = s.Sum(c => c.price) });
}
I hope this isn't too complicated ^^'我希望这不是太复杂^^'
Try:尝试:
unitOfWork.Contracts.GetAll()
.Where(c => c.create_date.Year == year && c.create_date.Month == month)
.Sum(c => c.price);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.