[英]LINQ - EF - FROM or JOIN
I use c#, linq and EF4.我使用 c#、linq 和 EF4。
I have two tables in my DataBase represented in my Conceptual Model:我的数据库中有两个表,用我的概念 Model 表示:
DataBase Tables:数据库表:
CmsContents
CmsCategories
CmsRelatedCategories (Pure Juction Table)
Entity Type:实体类型:
CmsContent
CmsCategory
Entyt Set:实体集:
CmsContents
CmsCategories
I have some Navigational Properties:我有一些导航属性:
for CmsContents --> CmsCategories --> Return Collection of Cms CmsCategory
for CmsCategories --> CmsContents --> Return Collection of Cms CmsContents
I need query Entity Framework to retrive a list of Categories for a selected Content not associated in the Junction table.我需要查询实体框架来检索未在联结表中关联的选定内容的类别列表。
At the moment I use this code (note the two FROM):目前我使用这段代码(注意两个 FROM):
var categories = from category in context.CmsCategories
from content in category.CmsContents
select category;
That Returns a list of all category not associated in the entire database and not to a specific Content.返回在整个数据库中未关联的所有类别的列表,而不是特定内容。
I need show the list of Categories not associates with a SPECIFIC Content我需要显示与特定内容不相关的类别列表
Could you please write me the LINQ query so I can have a clear picture.您能否给我写下 LINQ 查询,以便我有清晰的图片。 Thanks fro your help.谢谢你的帮助。
You could try:你可以试试:
CmsCategories.Except(myContent.CmsCategories)
myContent is an instance of CmsContent, assuming you retrieved it before and you have lazy loading enabled. myContent 是 CmsContent 的一个实例,假设您之前检索过它并且您启用了延迟加载。
Edit: a possible sample of code:编辑:可能的代码示例:
var myContent = context.CmsContents.FirstOrDefault(c => c.Id == 1);
var contentCategories = myContent.CmsCategories;
var otherCategories = context.CmsCategories.Except(contentCategories);
foreach (var item in otherCategories)
{
//whatever you need to do
}
I think you want to select from the CmsCategories
and exclude anything that is in the associative table.我认为您想从 CmsCategories 中CmsCategories
并排除关联表中的任何内容。
var query =
context
.CmsCategories
.Except(context.CmsRelatedCategories.Select(x=>x.CmsCategory));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.