[英]Order date ascending
基本上我有一个带有支出和收入以及日期列的BalanceUsers模型,我想要的是查询该表并按日期升序排列所有行,我需要这样做,以便可以在我发送的startDate和endDate之间进行搜索一种在两个日期之间限制值的表格,但是什么也没发生,也许我做错了。
这是我所做的
[HttpPost]
public ActionResult Index(string startDate, string endDate)
{
DateTime data1 = DateTime.Parse(startDate);
DateTime data2 = DateTime.Parse(endDate);
var userId = User.Identity.GetUserId();
decimal gastos = 0;
decimal rendimentos = 0;
var orderDates = db.BalanceUsers.Where(d => d.ApplicationUserId == userId).OrderBy(d => d.data); //this is what does the job
var lowestValue = orderDates.Where(d => d.ApplicationUserId == userId).Min(d => d.valor);
var BiggestDate = orderDates.Where(d => d.ApplicationUserId == userId).First(d => d.valor == lowestValue);
var dateBiggestDate = BiggestDate.data;
var biggestValue = orderDates.Where(d => d.ApplicationUserId == userId).Max(d => d.valor);
var biggestDate2 = orderDates.Where(d => d.ApplicationUserId == userId).First(d => d.valor == biggestValue);
var biggestDateEarning = biggestDate2.data;
foreach (var balance in orderDates.Where(d => d.ApplicationUserId == userId))
{
if(balance.valor < 0)
{
expenses += balance.valor;
}
else
{
earnings += balance.valor;
}
}
statistic model = new statistic()
{
utilizador = User.Identity.GetUserName(),
gastos = gastos,
rendimentos = rendimentos,
maiorValorDespesa = lowestValue,
dataMaiorDespesa = dataMaiorDespesa,
dataMaiorRendimento = dataMaiorRendimento,
maiorValorRendimento = biggestValue,
};
return View(modelo);
在我看来,我只是显示传递给我的ModelView的数据,所以我认为问题不在视图上。
PS:对不起,我英语不好
下次或如果您需要更多帮助
就是说,我查看了您的代码,似乎您只需一次调用就可以完成所有操作。 另外,在代码中没有实际使用传递的日期的原因。为什么不将它们作为实际的DateTime
对象传递?
var highestLowestValors = db.BalanceUsers.Where(d => d.ApplicationUserId == userId)
.GroupBy(x => x.ApplicationUserId)
.Select(x => new {
biggestValor = x.Max(y => y.valor),
lowestValor = x.Min(y => y.valor),
expenses = x.Where(y => y.valor < 0).Sum(y => y.valor),
earnings = x.Where(y => y.valor > 0).Sum(y => y.valor),
}).Single();
我需要这样做,以便我可以在startDate和endDate之间搜索
First Sql是一种声明性语言。 这意味着您无需排序数据,然后遍历数据即可获得所需的内容。 而是声明您只需要这两个日期之间的数据。 比方说你的表的日期列BalanceUsers
被称为OccurredOn
,你会改变查询像这样。
DateTime startDateSearch, endDateSearch; // assign these values somewhere
var highestLowestValors = db.BalanceUsers.Where(d => d.ApplicationUserId == userId && d.OccurredOn >= startDateSearch && d.OccurredOn <= endDateSearch)
.GroupBy(x => x.ApplicationUserId)
.Select(x => new {
biggestValor = x.Max(y => y.valor),
lowestValor = x.Min(y => y.valor),
expenses = x.Where(y => y.valor < 0).Sum(y => y.valor),
earnings = x.Where(y => y.valor > 0).Sum(y => y.valor),
}).Single();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.