实体框架选择相关实体的集合作为自己的列表

时间:2013-12-18 22:44:02

标签: c# entity-framework-5

以下是此问题的数据库表: enter image description here

鉴于我已经选择了WorkReleaseHeader,我现在想要获得与该WorkReleaseHeader相关的WorkOrderDetails列表。似乎很容易,但我无法做到这一点。我的可行尝试:

List<WorkReleaseDetail> workReleaseDetails =
((IEnumerable<WorkReleaseDetail>)wrkRlsHdr.WorkReleaseDetails).ToList();

List<WorkOrderDetail> workOrderDtlsForWorkRelease = 
  workReleaseDetails
  .Select(wrkDtls => wrkDtls.WorkOrderHeader.WorkOrderDetails).ToList();

导致Select语句行错误:

Error 3 Cannot implicitly convert type
'System.Collections.Generic.List<System.Collections.Generic.ICollection<WorkOrderDetail>>'
 to 'System.Collections.Generic.List<WorkOrderDetail>'

1 个答案:

答案 0 :(得分:1)

我认为这应该有效:

List<WorkOrderDetail> workOrderDtlsForWorkRelease = 
  workReleaseDetails
  .SelectMany(wrkDtls => wrkDtls.WorkOrderHeader.WorkOrderDetails).ToList();

LINQ中的SelectMany可以使您的列表变平,因此在您的示例中,您将没有列表集合。