Order By Doubt with LINQ to generate report

Asked

Viewed 39 times

-2

I’m making a report with the iTextSharp and I order by date but when I view the report the company Empresa2XYZ appears 2 times with the same date.

Follow the code with LINQ.

lista = context.TB_ContasPagar
                .Where(c => DbFunctions.TruncateTime(c.DataBaixa) >= dataInicial.Date && DbFunctions.TruncateTime(c.DataBaixa) <= dataFinal.Date)
                .OrderBy(c => DbFunctions.TruncateTime(c.DataBaixa)).ToList();
                //.ThenBy(c => c.ID_EmpresaConta).ToList();

I tried to use the ThenBy but it didn’t solve the problem.

Follow picture

inserir a descrição da imagem aqui

  • managed to come to some conclusion?

  • I suggest you improve the description of the problem by explaining the context. The report should not also be ordered by company?

1 answer

-1


It’s really hard to answer without the full context, but I’ll try to guide you through how to solve this problem.

The first thing to do would be to remove the DbFunctions.TruncateTime(c.DataBaixa). They seem unnecessary. If you do not want to take into account the time, your search should be by the date with the times at 00:00 and 23:59:59. The same for the OrderBy, unless it is strictly necessary that his return be ordered by the date, ignoring the time, and then by another property (such as the name of the company), but still could follow a different approach, if any (it does not seem to be, given his attempt with ThenBy(c => c.ID_EmpresaConta)). Also change the report display to include the time, to make it easier to understand the problem. This will not solve your problem yet, but should improve your query and facilitate data visualization.

Before trying to see the problem in the report, see the values in lista. I imagine there are four entrances to Empresa2XYZ. To make sure these entries are together in your result .OrderBy(c => DbFunctions.TruncateTime(c.DataBaixa)).ThenBy(c => c.ID_EmpresaConta).ToList(); should actually solve the problem, but without having the full context of your data and data model, I can’t figure out why it doesn’t work. Again this still doesn’t solve the problem, but it helps you try to identify it.

Finally, in order to ensure that the resolutions are grouped together, you could use the GroupBy and group by date (without time in this case) and ID or nome of the company. This can be done so .GroupBy(x => new { x.Column1, x.Column2 }), where Column1 would be your date, no time, and Column2 a unique identifier for enterprises.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.