4
Why the exception below occurs when using Split
within a Select
in a IQueryable
?
The LINQ Expression Node type 'Arrayindex' is not supported in LINQ to Entities.
I already managed to solve the problem using ToList
and doing the Select
with Split
in it, but I would like to understand what is the reason for the problem.
Thus occurs exception:
var retorno = entities.tabela.Where(x => x.coluna1 == null)
.Select(x => new { Campo1 = x.coluna1, Campo2 = x.coluna2 });
retorno = retorno.Select(x => new { x.Campo1, Campo2 = x.Campo2.Split(' ')[0] });
That’s how it works:
var retorno = entities.tabela.Where(x => x.coluna1 == null)
.Select(x => new { Campo1 = x.coluna1, Campo2 = x.coluna2 }).ToList();
retorno = retorno.Select(x => new { x.Campo1, Campo2 = x.Campo2.Split(' ')[0] }).ToList();
How cool! I thought the darlings were executed whenever I wore
Select
orWhere
. This explains another problem I had when I tried to joinIQueryable
andList
.– Daniel Dutra
@Danieldutra You see more details, in the answer here (which is also from Gypsy)
– Jéf Bueno
@jbueno the Gypsy already linkou she in response, vlw!
– Daniel Dutra
Ouch. I had not seen, thanks for warning.
– Jéf Bueno
Just confirm me something that I don’t think is worth a new question: ask a
foreach
innew List<string>().Select(...)
andnew List<string>().Select(...).ToList()
is the same, right? In this case theSelect
is actually executed when I "call him", right?– Daniel Dutra
Ops, in fact, is rather the same thing. Had not attempted that the example used a list. If you do the
Select
in a list, yes it’s pretty much the same thing. The only difference is that without theToList()
iteration will be done in aIEnumerable
, with theToList()
it is made in a list. By the way, I think it is worth a question.– Jéf Bueno
http://answall.com/questions/129779/foreach-em-select-e-list !
– Daniel Dutra