Linq C# with order

Asked

Viewed 92 times

3

I have a LINQ query that lists the current month’s birthday:

        Ramal[] RamalAniver = (from a in db.Ramais
                               where a.sAniversario.Contains(DiaAniversario + "/" + MesAniversario) && a.bAtivo == true
                               select a)
                               .Union(
                               from c in db.Ramais
                               where c.sAniversario.Contains("/" + MesAniversario) && c.bAtivo == true
                               orderby c.sAniversario
                               select c
                               ).ToArray();

But when I put it together, the intention was to bring the birthday boy of the day as the first record, and in the second the current birthday boy. The query is returning the records but the birthday of the day does not come first...

When the list is assembled the order is not maintained... how to make it maintain the order I put in the Union?

  • 2

    Just get the order out of the Union

3 answers

0

I searched and put together the result I got the solution:

I simplified the query by bringing only the birthday of the month:

Extension[] Extension = (from c in db. Extension Where c.sAniversario.Contains("/" + Mesaniversary) && c.bAtivo == true orderby c.sAniversario select c ). Toarray();

and using this expression I managed to leave the day’s birthday in first position and then in order by day of the month:

Ramalaniver = Ramalaniver.Orderbydescending(x => x.sAniversario == Diaaniversario + "/" + Mesaniversario). Thenby(x => x.sAniversario). Toarray();

If anyone can detail the understanding of this expression, thank you.

0

Whereas you can convert birthday dates into Datetime we have the following solution:

var dataHoje = DateTime.Now;
var aniversariantesDoMes =  pessoas.Where(p => p.DataNascimento.Month == dataHoje.Month && p.DataNascimento.Day >= dataHoje.Day )
        .OrderBy(d => new DateTime(dataHoje.Year,d.DataNascimento.Month,d.DataNascimento.Day));

0

Linq does not guarantee order maintenance, so you can use a trick.

Ramal[] RamalAniver =
    (from a in db.Ramais
    where a.sAniversario.Contains(DiaAniversario + "/" + MesAniversario) && a.bAtivo == true
    select new { Ramal = a, Ordem = 0 })
    .Union(
    from c in db.Ramais
    where c.sAniversario.Contains("/" + MesAniversario) && c.bAtivo == true
    orderby c.sAniversario
    select new { Ramal = c, Ordem = 1 })
    .OrderBy(n => n.Ordem)
    .Select(n => n.Ramal)
    .ToArray();

It’s okay that you’re adding a little more complexity, but so you stay within the context of Linq, with its benefits.

Basically you would be creating a new type with 2 property, one is the Ramal and the other is a int with the order imposed by your model, and, after using it to order the result, throwing that helper out to return only the data you want.

I believe this will solve your problem.

Browser other questions tagged

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