The specified type member '.....' is not supported in LINQ to Entities

leo del ciello 66 Reputation points
2021-11-03T16:35:50+00:00

I am trying the following Linq query :

from
                        codiciSegnalazioni in _context.Set<CodiciSegnalazioni>()
                                                            .Where(cs => cs.Codice.Equals(segnalazioni.CodiciSegnalazioniCodice))
                                                            .DefaultIfEmpty()
                    from
                        localita in _context.Set<LocalitaVersioni>()
                                                            .Where(lv => lv.CodiceLocalitaPdP.Equals(segnalazioni.LocalitaCodiceLocalitaPdPInizio) &&
                                                                         lv.DataInizioValidita.CompareTo(dataOraRichiesta.Date) <= 0 &&
                                                                         lv.DataFineValidita >= dataOraRichiesta.Date)
                                                            .DefaultIfEmpty()
                    from treniGO in _context.Set<TreniGO_TreniProduzioneGO>()
                                                            .Where(tg => tg.Codice.Equals(segnalazioni.TreniProduzioneGOCodice))
                                                            .DefaultIfEmpty()
                    where
                        segnalazioni.DataOraRilevazione >= dataOraSoglia

                    orderby
                        segnalazioni.DataOraRilevazione descending
                    select new
                    {
                        segnalazioni.TreniProduzioneGOCodice,
                        segnalazioni.TreniProduzioneGODataPartenza,
                        segnalazioni.CodiciSegnalazioniCodice,
                        DescrizioneSegnalazione = (codiciSegnalazioni == null) ? String.Empty : codiciSegnalazioni.Descrizione,
                        segnalazioni.Nota,
                        DescrizioneLocalita = (localita == null) ? String.Empty : localita.Descrizione40,
                        CodiceLocalita = (localita == null) ? String.Empty : localita.CodiceLocalitaPdP,
                        segnalazioni.DataOraRilevazione,
                        NumeroTrenoCommerciale = (treniGO == null) ? String.Empty : treniGO.NumeroTrenoCommerciale //?????
                    }

I am getting the following error :

"The specified type member NumeroTrenoCommerciale is not supported in LINQ to Entities . .Only initializers, entity members, and entity navigation properties are supported."

Seems to be related to the presence of treniGO.NumeroTrenoCommerciale on the last line of the query : if I remove the leftvalue and substitute it with a "" everything works.
The Entity TreniGO_TreniProduzioneGO , to which treniGO belongs, contains the field NumeroTrenoCommerciale, which is correctly mapped to the corresponding Table column in the realted mapping file :

Entity property :

[DataMember()]
public string NumeroTrenoCommerciale { get; set; }

Table mapping :

this.Property(t => t.NumeroTrenoCommerciale)
.HasColumnName("NumeroTrenoCommerciale")
.IsUnicode(false)
.HasMaxLength(20);

Where is the error ?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,090 questions
{count} vote

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.