How to include() nested child entity in linq

前端 未结 4 392
日久生厌
日久生厌 2020-12-03 04:17

How do I include a child of a child entitiy?

Ie, Jobs have Quotes which have QuoteItems

var job = db.Jobs
            .Where(x => x.JobID == id)
          


        
4条回答
  •  一整个雨季
    2020-12-03 05:09

    To get a job and eager load all its quotes and their quoteitems, you write:

    var job = db.Jobs
            .Include(x => x.Quotes.Select(q => q.QuoteItems))
            .Where(x => x.JobID == id)
            .SingleOrDefault();
    

    You might need SelectMany instead of Select if QuoteItems is a collection too.

    Note to others; The strongly typed Include() method is an extension method so you need to include using System.Data.Entity; at the top of your file.

提交回复
热议问题