locked
Problem with sum of values from a cell table RRS feed

  • Question

  • User-1822150920 posted

    From my DB I want to calculate the sum of the sales recorded of the week, but the query only shows me the sale of the 7th day (It doesn't sum the sales of the 6th, 5th, 4th, ..., day).

    My crtiteria to evaluate this is get the sales between the current date and the 7th day back.

    This is my query:

    var queryVtasFact = (from c in context.VentasDias
    			where c.TenantId.ToString() == "2e36327a-f150-460a-8c39-069fafaccf6e" && c.SucursalId == 1 && (c.Fecha >= semana && c.Fecha < dia) 
    			group c by c.Fecha into aux
    			select new
                                     {
                                         VtasFac = aux.Sum(f => f.VentasFacturadas)
                                     }).FirstOrDefault();

    And this is how I get the restrictions of the dates:

    DateTime dia = DateTime.Today;
    DateTime semana = DateTime.Today.AddDays(-7);

    Today is Friday, April 3rd., 2015. In theory, the query will should sum the sales of April 2nd and 1st, and from the March 31rd to March 27th. The query only displays the sale of March 27th. 

    I'll appreciate any help.

    Friday, April 3, 2015 5:30 PM

Answers

  • User-419997905 posted

    Hi,

    You could do something like this

    var queryVtasFact = (from c in context.VentasDias
    			where c.TenantId.ToString() == "2e36327a-f150-460a-8c39-069fafaccf6e" && c.SucursalId == 1 && EntityFunctions.DiffDays(DateTime.Now, c.Fecha) <= 7
    			group c by c.Fecha into aux
    			select new
                                     {
                                         VtasFac = aux.Sum(f => f.VentasFacturadas)
                                     }).FirstOrDefault();

    Let me know if you still face any issues.

    Thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 6, 2015 3:50 AM
  • User61956409 posted

    Hi ArCiGo,

    Thanks for your post.

    As sandipnehate said, you could try to use DiffDays( startExpression, endExpression) Method that could return the difference, in days, between startExpression and endExpression. For more information, please refer to this.

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 7, 2015 11:00 PM

All replies

  • User-419997905 posted

    Hi,

    You could do something like this

    var queryVtasFact = (from c in context.VentasDias
    			where c.TenantId.ToString() == "2e36327a-f150-460a-8c39-069fafaccf6e" && c.SucursalId == 1 && EntityFunctions.DiffDays(DateTime.Now, c.Fecha) <= 7
    			group c by c.Fecha into aux
    			select new
                                     {
                                         VtasFac = aux.Sum(f => f.VentasFacturadas)
                                     }).FirstOrDefault();

    Let me know if you still face any issues.

    Thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 6, 2015 3:50 AM
  • User61956409 posted

    Hi ArCiGo,

    Thanks for your post.

    As sandipnehate said, you could try to use DiffDays( startExpression, endExpression) Method that could return the difference, in days, between startExpression and endExpression. For more information, please refer to this.

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 7, 2015 11:00 PM