简体   繁体   English

订单日期升序

[英]Order date ascending

Basicly i have a model BalanceUsers with expendes and earnings and a date column, what i want is query that table and order all the rows by date ascending, i need to do that soo i can search between a startDate and an endDate that i send from a form to get values restricted between this two dates, but nothing happened maybe i did something wrong. 基本上我有一个带有支出和收入以及日期列的BalanceUsers模型,我想要的是查询该表并按日期升序排列所有行,我需要这样做,以便可以在我发送的startDate和endDate之间进行搜索一种在两个日期之间限制值的表格,但是什么也没发生,也许我做错了。

Here is what i did 这是我所做的

[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);

In my view i just display the data that i passed to my modelView, so i dont think the problem is on the view. 在我看来,我只是显示传递给我的ModelView的数据,所以我认为问题不在视图上。

PS: Sorry for my bad english PS:对不起,我英语不好

For next time or if you want more help 下次或如果您需要更多帮助

  1. It would really help if you did some translating of your code to English. 如果您将代码翻译成英文,那确实有帮助。 I have no idea what the properties actually mean so I am going to take some guesses here based on what you have written. 我不知道这些属性的实际含义,因此我将根据您所写的内容进行一些猜测。
  2. It would also help if you provided your model for the object you are working with in your method. 如果您为方法中使用的对象提供了模型,也将有所帮助。
  3. Finally it would help if you described what it is you are trying to accomplish. 最后,如果您描述了您要实现的目标,这将有所帮助。

That said I looked at your code and it seems you can do everything in there with just 1 call. 就是说,我查看了您的代码,似乎您只需一次调用就可以完成所有操作。 Also no where in your code do you actually make use of those dates that are passed in. Also why are you not passing them in as actual DateTime objects? 另外,在代码中没有实际使用传递的日期的原因。为什么不将它们作为实际的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();

i need to do that soo i can search between a startDate and an endDate 我需要这样做,以便我可以在startDate和endDate之间搜索

First Sql is a declaritive language. First Sql是一种声明性语言。 This means you do not need to order the data and then loop through it to get what you need. 这意味着您无需排序数据,然后遍历数据即可获得所需的内容。 Instead declare that you only want data between those 2 dates. 而是声明您只需要这两个日期之间的数据。 Lets say your date column for your table BalanceUsers is called OccurredOn , you would change the query like so. 比方说你的表的日期列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