简体   繁体   中英

Entity Framework Select Collection of Related Entities as Their Own List

Here is my db tables for this question: 在此处输入图片说明

Given that I have already selected a WorkReleaseHeader, I now want to get a list of the WorkOrderDetails related to that WorkReleaseHeader. Seems easy enough, but I'm not able to achieve this. My feable attempt:

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

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

Results in the error on the Select statement line:

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

I think this should work :

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

The SelectMany in LINQ can flat your list so in your example you won't have a collection of list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM