繁体   English   中英

订单日期升序

[英]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:对不起,我英语不好

下次或如果您需要更多帮助

  1. 如果您将代码翻译成英文,那确实有帮助。 我不知道这些属性的实际含义,因此我将根据您所写的内容进行一些猜测。
  2. 如果您为方法中使用的对象提供了模型,也将有所帮助。
  3. 最后,如果您描述了您要实现的目标,这将有所帮助。

就是说,我查看了您的代码,似乎您只需一次调用就可以完成所有操作。 另外,在代码中没有实际使用传递的日期的原因。为什么不将它们作为实际的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM