开发者

EF - Linq to Entity and navigation property

开发者 https://www.devze.com 2023-02-08 19:01 出处:网络
I need design a simple LINQ to Entity query using EF. I am pretty new at it and I am stuck for more of 1 day.

I need design a simple LINQ to Entity query using EF. I am pretty new at it and I am stuck for more of 1 day.

From my DataBase table are:

CmsJobs
CmsJobsContents (Pure Junctional Table)
CmsContents  

I need list a series of CmsContents with a specific CmsJobs.JobId

Any idea how to do it? Thanks for your开发者_StackOverflow社区 help

My Model EF:

EF - Linq to Entity and navigation property


CmsJob job = (from j in dataContext.CmsJobs where j.JobId == jobIdIAmLookingFor select j).FirstOrDefault();   
IEnumerable<CmsContent> theContentItems = job.CmsContents;


Or more readible and faster (I recently did a lot of tests to query the model from one way or the other (fromout a CmsJobs perspective or fromout the CmsContents perspective):

    using(EntityModel context = new EntityModel())
    {
      List<CmsContents> list = context.CmsContents
                               .Include("CmsJobs")
                               .Where<CmsContents>(cc => cc.CmsJobs.Where<CmsJobs>(cj => cj.JobId == requiredId))
                               .ToList<CmsContents>()
    }

Did not test this, but should work I believe. Try it out yourself. It's the less logical way of querying perspective but gives you exactly what you need (a list of CmsContents entities) without the containing CmsJobs entity (as supposed by Rune).

0

精彩评论

暂无评论...
验证码 换一张
取 消