简体   繁体   English

EF-实体和导航属性的Linq

[英]EF - Linq to Entity and navigation property

I need design a simple LINQ to Entity query using EF. 我需要使用EF设计一个简单的LINQ to Entity查询。 I am pretty new at it and I am stuck for more of 1 day. 我对此很陌生,并且停留了1天以上。

From my DataBase table are: 从我的数据库表中是:

CmsJobs
CmsJobsContents (Pure Junctional Table)
CmsContents  

I need list a series of CmsContents with a specific CmsJobs.JobId 我需要列出一系列具有特定CmsJobs.JobId的CmsContents

Any idea how to do it? 知道怎么做吗? Thanks for your help 谢谢你的帮助

My Model EF: 我的EF模型:

在此处输入图片说明

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): 或更可读, 更快 (我最近做了很多测试,以一种或另一种方式查询模型(从CmsJobs角度或从CmsContents角度来看):

    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). 这是查询透视图的较不合逻辑的方法,但是可以在不包含CmsJobs实体(如Rune假定)的情况下,为您提供所需的确切信息 (CmsContents实体列表)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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