C# Linq至实体”的定义;在;具有多个关系的查询?

C# Linq至实体”的定义;在;具有多个关系的查询?,c#,entity,C#,Entity,我有这个数据库结构 我正在尝试加载产品属性列表,并包含单个产品的产品属性项 因此,产品属性通过产品属性映射映射,然后单个产品属性映射也被映射 我的头绕不过去!我猜应该是某种IN语句,即选择映射表中存在的项 我尝试过使用各种join语句,但总是在列表中出现更多Product\u Attrubutes,每个语句只包含一个Product\u AttributeItem 因此,不要有3个产品属性(尺寸、颜色、风格);包含项目(S、M、L)的大小属性,包含项目(黑色、白色)的颜色属性;包含(US)的样式属

我有这个数据库结构 我正在尝试加载产品属性列表,并包含单个产品的产品属性项

因此,产品属性通过产品属性映射映射,然后单个产品属性映射也被映射

我的头绕不过去!我猜应该是某种IN语句,即选择映射表中存在的项

我尝试过使用各种join语句,但总是在列表中出现更多Product\u Attrubutes,每个语句只包含一个Product\u AttributeItem

因此,不要有3个产品属性(尺寸、颜色、风格);包含项目(S、M、L)的大小属性,包含项目(黑色、白色)的颜色属性;包含(US)的样式属性。最后,我将得到3个尺寸属性,每个属性包含1个尺寸,2个颜色属性,每个属性包含1个颜色,以及1个样式属性(如果有意义的话)

编辑@Daniel Hilgarth

代码与此类似

List<Product_Attribute> attributes = 
(from a in     _db.Product_Attribute.Include("Product_AttributeItem")
join item in _db.Product_AttributeItem on a.AttributeId equals item.AttributeId
join aim in _db.Product_AttributeItemMap on item.AttributeItemId equals
aim.AttributeItemId where item.AttributeId == a.AttributeId
select a).ToList();
列表属性=
(来自数据库中的产品属性Include(“产品属性项”)
在a.AttributeId等于item.AttributeId的_db.Product_AttributeItem中加入项
在item.AttributeItemId equals上的_db.Product_AttributeItemMap中加入aim
aim.AttributeItemId,其中item.AttributeId==a.AttributeId
选择一个.ToList();
结果是列出5个产品属性,而不是现有的3个(尺寸、颜色、样式)

编辑@Botz3000

往另一个方向走

List<Product_AttributeItems> attributeItems =  
( from aim in   _db.Product_AttributeItemMap.Include("Product_AttributeItem.Product_Attribute") 
where aim.ProductId == prodId 
select aim).ToList();
列表属性项=
(来自_db.Product\u AttributeItemMap.Include(“Product\u AttributeItem.Product\u Attribute”)中的aim)
其中aim.ProductId==prodId
选择aim).ToList();

我最终得到了我需要的数据,但格式不对。由于我需要它作为包含其产品属性项(用于选项)的产品属性列表(每个属性都是下拉列表)

,因此听起来您的查询方向不对

您可以选择产品的属性项和产品的属性,然后将这两个属性合并

类似这样的事情(可能不是完全像这样,只是一般的想法):

如果需要每个属性的可用选项,可以尝试以下方法:

from att in Product.AttributeMap.Select(m => m.Attribute).Include("AttributeItems")
select att

请发布您尝试的代码,以便我们可以改进它。
from att in Product.AttributeMap.Select(m => m.Attribute).Include("AttributeItems")
select att