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:
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).
精彩评论