Error when simulating distinc in query with Linq and lambda expression

2

I have 2 tables in a SqlServer database. One call to another and another. I'm trying to return all states of the tbState table that have at least one city (tbCity) and with the properties tbCity.bnlActivityCity and tbCity.blnExibirNoPortal = true. The result should be sorted alphabetically by state name.

I'm trying like this:

IQueryable<tbEstado> ListaDeEstados = ctx.tbEstado
            .Join(ctx.tbCidade, estado => estado.idEstado, cidade => cidade.idEstado, (estado, cidade) => new { estado, cidade })
            .Where(e => e.cidade.bnlAtivarCidade == true && e.cidade.blnExibirnoPortal == true)
            .Select(e => e.estado)
            .GroupBy(e => e.idEstado)
            .Select(group => group.First()).OrderBy(e => e.txtNomeEstado);

But I'm getting the following error:

  

Additional information: The 'First' method can only be used as a final query operation. Consider using the method 'FirstOrDefault' in this instance instead.

Could someone help?

    
asked by anonymous 10.01.2018 / 18:34

1 answer

1

To search for all states that are associated with the city, search the city repository.

Ex.

ctxCidade.Where(...).Select(x => x.Estado).Distinct().ToList();

Send your Entities City and State. There seems to be something wrong there.

    
10.01.2018 / 18:37