Italo Rodrigo
Italo Rodrigo

Reputation: 1785

How to convert select to linq without error of conversion?

I want to convert the code below to LINQ

SELECT bpac.cmp FROM bpac
UNION
SELECT bpai.cmp FROM bpai
GROUP BY cmp
ORDER BY cmp DESC

I am trying to get the first table but I get an error:

List<string> listaBpac = modelOff.bpacs.Where(p => p.ibge == oUsuario.ibge)
                                 .Select(p => new { p.cmp })
                                 .ToList();

Error:

Cannot implicitly convert type 'system.collections.generic.list "anonymous type: string cmp"' to 'system.collections.generic.list "anonymous type: string"'

Upvotes: 0

Views: 26

Answers (1)

Phill
Phill

Reputation: 1423

Try;

List<string> listaBpac = modelOff.bpacs.Where(p => p.ibge == oUsuario.ibge)
                                    .Select(p => p.cmp)
                                    .ToList();

(you really need don't need the "new" keyword if you're returning a string member)

Upvotes: 1

Related Questions