Il seguente LINQ:nidificato LINQ restituzione di un tale metodo non può essere tradotto in un'eccezione negozio di espressione
retval = (from jm in entities.JobMasters
where jm.UserId == userId && jm.IsRemote == false
select new JobDto
{
JobMasterId = jm.JobMasterId,
ExternalTaskId = jm.ExternalTaskId,
JobDetails = (from jd in entities.JobDetails
where jd.JobMasterId == jm.JobMasterId
select new JobDetailDto { ScreenFieldId = jd.ScreenFieldId, FieldValue = jd.FieldValue }
).ToList()
}
).ToList();
mi sta dando questo errore:
LINQ to Entities does not recognize the method 'System.Collections.Generic.List`1[KernWcfTest.DataTransferObjects.JobDetailDto] ToList[JobDetailDto](System.Collections.Generic.IEnumerable`1[KernWcfTest.DataTransferObjects.JobDetailDto])' method, and this method cannot be translated into a store expression.
Ecco i due dto classi:
[DataContract]
public class JobDetailDto
{
[DataMember]
public int ScreenFieldId { get; set; }
[DataMember]
public string FieldValue { get; set; }
}
[DataContract]
[KnownType(typeof(JobDetailDto))]
public class JobDto
{
[DataMember]
public int JobMasterId { get; set; }
[DataMember]
public string ExternalTaskId { get; set; }
[DataMember]
public List<JobDetailDto> JobDetails { get; set; }
}
Il problema è la sottoselezione e l'elenco JobDetails. Ho provato ad aggiungere il KnownType ma non ha funzionato.
Questo funziona perfettamente in LINQ Pad.
Qualche idea?
Acclamazioni
Steve
Penso che stiate andando nella direzione giusta, ma che mi dà Impossibile convertire implicitamente il tipo 'System.Linq.IQueryable' a System.Collections.Generic.List '. –
Quale errore si ottiene (se presente) se si modifica l'ultima .ToList() in .AsEnumerable(). ToList()? –
Il problema ora è che il tuo DTO non si adatta bene alle tue domande. Consenti al tuo DTO di accettare un oggetto IEnumerable o di eseguire query separate. –