Error using Split within Select

4

Why does the following exception occur when using Split within a Select in a IQueryable ?

  

The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities.

I've been able to solve the problem using ToList and doing Select with Split on it, but I'd like to understand what the problem is.

So exception occurs:

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] });

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();
    
asked by anonymous 20.05.2016 / 00:50

1 answer

2
  

I've been able to solve the problem using ToList and doing Select with Split on it, but I'd like to understand what the problem is.

O Reason begins in this explanation . Without resolving the enumeration, its IQueryable tries to pass Split to an equivalent in SQL, which does not exist.

It should be understood that DbSet implements both IQueryable and IEnumerable . When calling ToList() , you ask LINQ to resolve the enumeration, and so it executes SQL. As Split in the second example was for the collection already resolved, Select acts on the collection in memory, not more generation of SQL.

    
20.05.2016 / 01:13