The following LINQ:
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();
is giving me this error:
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.
Here are the two dto classes:
[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; }
}
The problem is the sub-select and the JobDetails list. I tried adding the KnownType but it didn't work.
This all works fine in LINQ Pad.
Any ideas?
Cheers
Steve
Don't call ToList on the inner query (the one for JobDetails). The error is "This .ToList method you speak of -- it can't be translated to T-SQL!"
This should work:
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();
Note that you can call ToList on the end of the query, as that part doesn't need to be translated to T-SQL.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With