以下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();
给了我这个错误:
LINQ to Entities无法识别该方法 “System.Collections.Generic.List`1 [KernWcfTest.DataTransferObjects.JobDetailDto] ToList [JobDetailDto](System.Collections.Generic.IEnumerable`1 [KernWcfTest.DataTransferObjects.JobDetailDto])'方法, 并且此方法无法转换为商店表达式。
以下是两个dto类:
[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; }
}
问题是子选择和JobDetails列表。我尝试添加KnownType但它没有用。
这一切在LINQ Pad中都能正常工作。
有什么想法吗?
干杯
史蒂夫
答案 0 :(得分:9)
不要在内部查询(JobDetails的那个)上调用ToList。错误是“你说的这个.ToList方法 - 它无法转换为T-SQL!”
这应该有效:
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,因为该部分不需要转换为T-SQL。