How to create a Lambda expression with Group by and Order By

Asked

Viewed 1,041 times

1

I’m having trouble creating a Lambda sentence with Group by and Order By Together.

How to group by column Scope and Idescope, and sort by Idescope.

Segue Sentença.

 public static List<Escopos> EscoposCalCliente(int IDCalCLiente)
        {
            using (entidadesIUS entidades = new entidadesIUS())
            {
                return entidades.Escopos.Where(e => e.CALClientes.Any(cc => cc.IDCALCliente == IDCalCLiente)).
                ToList().
                GroupBy(es => new {.IDEscopo, es.IDEscopo }).
                SelectMany(esc => esc.OrderBy(escp => escp.IDEscopo));
            }
        }

1 answer

3

I see no need in your example of using GroupBy and OrderBy. Only the OrderBy does all the work.

    public static List<Escopos> EscoposCalCliente(int IDCalCLiente)
    {
        using (entidadesIUS entidades = new entidadesIUS())
        {
            return entidades.Escopos
                .Where(e => e.CALClientes.Any(cc => cc.IDCALCliente == IDCalCLiente))
                .OrderBy(es => es.IDEscopo)
                .ToList();
        }
    }

Browser other questions tagged

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