Cyro Bergonzi
Cyro Bergonzi

Reputation: 13

Linq ToLookUp too slow

How can I streamline the LookUp command below?

I have this context:

var lookup = query.ToLookup(
                i => new { PaisId = i.PaisId, EmpresaId = i.EmpresaId, Codigo = i.Codigo, Nome = i.Nome, OrigemId = i.OrigemId },
                v => new HierarquiaUsuarioSimplesDto { PapelId = v.PapelId, HierarquiaPapelId = v.HierarquiaPapelId, Usuario = new HierarquiaUsuarioDto { Id = v.UsuarioId, Nome = v.UsuarioNome, Matricula = v.UsuarioMatricula, GrupoCargo = v.UsuarioGrupoCargo, GrupoCargoNome = v.GrupoCargoNome } }
            );

        var item = query.FirstOrDefault();

        return new HierarquiaDto
        {
            PaisId = item.PaisId,
            EmpresaId = item.EmpresaId,
            Codigo = item.Codigo,
            Nome = item.Nome,
            OrigemId = item.OrigemId,
            IsPendente = item.IsPendente,
            Usuarios = lookup.FirstOrDefault().ToList()
        };´

The query.ToLookup command is very slow because it returns a lot of data.

Upvotes: 1

Views: 351

Answers (1)

JamesHoux
JamesHoux

Reputation: 3457

I think the problem is you're using ToLookUp() incorrectly. ToLookup is used to build a dictionary of lists. The first parameter is used to create the key for each list entered into the dictionary.

To me, the code you've written looks like nothing you would normally do with ToLookUp()

Upvotes: 1

Related Questions