Error accessing array indexes inside a LINQ expression

0

I am doing a query to search all the meteorologies with a certain date, but when executing the following error occurs:

An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code

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

Follow the code below:

var metereologias = from b in db.Metereologias.Where(b => b.data_de_leitura.Equals(tmp[i]))
                                   select new MetereologiaDTO()
                                   {
                                       metereologiaId = b.metereologiaId,
                                       data_de_leitura = b.data_de_leitura,
                                       hora_de_leitura = b.hora_de_leitura,
                                       temperatura = b.temp,
                                       vento = b.vento,
                                       pressão = b.pressao,
                                       NO = b.NO,
                                       NO2 = b.NO2,
                                       CO2 = b.CO2,
                                       Local = b.Local.Nome
                                   };
                List<MetereologiaDTO> tmpResult = new List<MetereologiaDTO>(metereologias);
    
asked by anonymous 13.11.2016 / 18:19

1 answer

1
To fix the problem use a temporary variable to store the value at index i of array tmp :

var tempValue = tmp[i];
var metereologias = db.Metereologias.Where(b => b.data_de_leitura.Equals(tempValue))
                                    .Select(new MetereologiaDTO
                                    {
                                       metereologiaId = b.metereologiaId,
                                       data_de_leitura = b.data_de_leitura,
                                       hora_de_leitura = b.hora_de_leitura,
                                       temperatura = b.temp,
                                       vento = b.vento,
                                       pressão = b.pressao,
                                       NO = b.NO,
                                       NO2 = b.NO2,
                                       CO2 = b.CO2,
                                       Local = b.Local.Nome
                                    }).ToList();

var result = new List<MetereologiaDTO>(metereologias);

If you use a tmp[i] array index within an expression tree, it will attempt to convert this to an expression as well, leading to an error.

UPDATED

I updated the code by adopting a pattern - making all the commands through the interface fluent, but the way you wrote your code will work the same.

    
13.11.2016 / 18:47