Linq查询以获取产品列表,每个产品都有一个标识符列表和一个属性列表
我正在尝试创建一个带有公共Linq查询以获取产品列表,每个产品都有一个标识符列表和一个属性列表,linq,entity-framework-core,Linq,Entity Framework Core,我正在尝试创建一个带有公共ProductGroupId的产品列表。每个产品都有任意数量的标识符(如产品编号、EAN、ISBN等)和任意数量的属性(如颜色、高度、大小等)。每个属性都有任意数量的可选选项(如颜色的“红色”、“绿色”等) 产品存储在产品中 标识符标签(“EAN”、“ISBN”等)存储在ProductIdentifiers中,标识符的产品值存储在IdentifierForProducts中。例如:EAN(标签):7044304034373(值) 属性标签(例如“颜色”)存储在Produ
ProductGroupId
的产品列表。每个产品都有任意数量的标识符(如产品编号、EAN、ISBN等)和任意数量的属性(如颜色、高度、大小等)。每个属性都有任意数量的可选选项(如颜色的“红色”、“绿色”等)
产品存储在产品中
标识符标签(“EAN”、“ISBN”等)存储在ProductIdentifiers
中,标识符的产品值存储在IdentifierForProducts
中。例如:EAN(标签):7044304034373(值)
属性标签(例如“颜色”)存储在ProductProperties
中,属性的可用选项(“红色”、“绿色”等)存储在ProductPropertyOptions
中,特定属性的产品所选选项id存储在products
中
以下是模型:
产品
public class Product
{
public int Id { get; set; }
public int ProductGroupId { get; set; }
public int ProductGroupSortOrder { get; set; }
public string Title { get; set; }
public string Info { get; set; }
public string LongInfo { get; set; }
public decimal Price { get; set; }
public int Weight { get; set; }
public int ProductTypeId { get; set; }
// Selected property options for this product
public ICollection<PropertyOptionForProduct> ProductPropertyOptionForProducts { get; set; }
// Product identifiers (EAN, ISBN, product number, etc.)
public ICollection<IdentifierForProduct> IdentifierForProducts { get; set; }
// ... some more properties
}
公共类产品
{
公共int Id{get;set;}
public int ProductGroupId{get;set;}
public int ProductGroupSortOrder{get;set;}
公共字符串标题{get;set;}
公共字符串信息{get;set;}
公共字符串LongInfo{get;set;}
公共十进制价格{get;set;}
公共整数权重{get;set;}
public int ProductTypeId{get;set;}
//此产品的选定属性选项
公共ICollection ProductPropertyOptionForProducts{get;set;}
//产品标识符(EAN、ISBN、产品编号等)
公共ICollection IdentifierFormProducts{get;set;}
//…更多的财产
}
标识符
public class ProductIdentifier
{
public int Id { get; set; }
public string Label { get; set; }
public string Description { get; set; }
public int SortOrder { get; set; }
public List<IdentifierForProduct> ProductIdentifiers { get; set; }
}
public class IdentifierForProduct
{
public int Id { get; set; }
public int ProductId { get; set; }
public int ProductIdentifierId { get; set; }
public string Value { get; set; }
public ProductIdentifier ProductIdentifier { get; set; }
public Product Product { get; set; }
}
public class ProductProperty
{
public int Id { get; set; }
public string Label { get; set; } // E.g. "Battery capacity"
public string Description { get; set; }
public string Unit { get; set; } // E.g. "mA"
public bool AllowMultipleSelections { get; set; }
public int OptionType { get; set; } // string, single number or from-to range?
public int SortOrder { get; set; }
// A property can have multiple options
public List<ProductPropertyOption> Options { get; set; }
}
public class ProductPropertyOption
{
public int Id { get; set; }
public int ProductPropertyId { get; set; }
public int SortOrder { get; set; }
public string StringValue { get; set; }
public decimal NumberValue { get; set; }
public decimal RangeFrom { get; set; }
public decimal RangeTo { get; set; }
public ProductProperty Property { get; set; }
public ICollection<PropertyOptionForProduct> PropertyOptionForProducts { get; set; }
}
public class PropertyOptionForProduct
{
public int Id { get; set; }
public int ProductId { get; set; }
public int ProductPropertyId { get; set; }
public int ProductPropertyOptionId { get; set; }
// Nav.props.
public Product Product { get; set; }
public ProductPropertyOption ProductPropertyOption { get; set; }
}
公共类ProductIdentifier
{
公共int Id{get;set;}
公共字符串标签{get;set;}
公共字符串说明{get;set;}
公共int排序器{get;set;}
公共列表产品标识符{get;set;}
}
公共类标识符产品
{
公共int Id{get;set;}
public int ProductId{get;set;}
public int ProductIdentifierId{get;set;}
公共字符串值{get;set;}
公共产品标识符ProductIdentifier{get;set;}
公共产品产品{get;set;}
}
属性
public class ProductIdentifier
{
public int Id { get; set; }
public string Label { get; set; }
public string Description { get; set; }
public int SortOrder { get; set; }
public List<IdentifierForProduct> ProductIdentifiers { get; set; }
}
public class IdentifierForProduct
{
public int Id { get; set; }
public int ProductId { get; set; }
public int ProductIdentifierId { get; set; }
public string Value { get; set; }
public ProductIdentifier ProductIdentifier { get; set; }
public Product Product { get; set; }
}
public class ProductProperty
{
public int Id { get; set; }
public string Label { get; set; } // E.g. "Battery capacity"
public string Description { get; set; }
public string Unit { get; set; } // E.g. "mA"
public bool AllowMultipleSelections { get; set; }
public int OptionType { get; set; } // string, single number or from-to range?
public int SortOrder { get; set; }
// A property can have multiple options
public List<ProductPropertyOption> Options { get; set; }
}
public class ProductPropertyOption
{
public int Id { get; set; }
public int ProductPropertyId { get; set; }
public int SortOrder { get; set; }
public string StringValue { get; set; }
public decimal NumberValue { get; set; }
public decimal RangeFrom { get; set; }
public decimal RangeTo { get; set; }
public ProductProperty Property { get; set; }
public ICollection<PropertyOptionForProduct> PropertyOptionForProducts { get; set; }
}
public class PropertyOptionForProduct
{
public int Id { get; set; }
public int ProductId { get; set; }
public int ProductPropertyId { get; set; }
public int ProductPropertyOptionId { get; set; }
// Nav.props.
public Product Product { get; set; }
public ProductPropertyOption ProductPropertyOption { get; set; }
}
公共类ProductProperty
{
公共int Id{get;set;}
公共字符串标签{get;set;}//例如“电池容量”
公共字符串说明{get;set;}
公共字符串单元{get;set;}//例如“mA”
公共布尔允许多重选举{get;set;}
public int OptionType{get;set;}//字符串,单个数字还是从到范围?
公共int排序器{get;set;}
//属性可以有多个选项
公共列表选项{get;set;}
}
公共类ProductPropertyOption
{
公共int Id{get;set;}
public int ProductPropertyId{get;set;}
公共int排序器{get;set;}
公共字符串StringValue{get;set;}
公共十进制数字值{get;set;}
公共十进制范围从{get;set;}
公共十进制范围{get;set;}
公共产品属性{get;set;}
产品{get;set;}的公共ICollection属性选项
}
产品的公共类属性选项
{
公共int Id{get;set;}
public int ProductId{get;set;}
public int ProductPropertyId{get;set;}
public int ProductPropertyOptionId{get;set;}
//导航道具。
公共产品产品{get;set;}
public ProductPropertyOption ProductPropertyOption{get;set;}
}
我当前的弗兰肯斯坦怪物查询如下所示:
List<Product> GroupMembers = await (
from prod in _context.Products
join ident in _context.IdentifiersForProducts on prod.Id equals ident.ProductId
join prop in _context.PropertyOptionsForProducts on prod.Id equals prop.ProductId
where
prod.ProductGroupId == groupId &&
prod.Id == ident.ProductId &&
prod.Id == prop.ProductId
select prod)
.Distinct()
.OrderBy(o => o.ProductGroupSortOrder)
.ToListAsync();
List GroupMembers=wait(
来自prod in_context.Products
将ident连接到_context.identifiersforprod.Id上的产品等于ident.ProductId
将prop加入_context.propertyOptions for prod.Id上的产品等于prop.ProductId
哪里
prod.ProductGroupId==groupId&&
产品Id==标识产品Id&&
产品Id==产品Id
选择prod)
.Distinct()
.OrderBy(o=>o.ProductGroupSortOrder)
.ToListAsync();
我还尝试了一些更简单的方法:
List<Product> GroupMembers =
await _context.Products
.Include(i => i.IdentifierForProducts)
.ThenInclude(i => i.ProductIdentifier)
.Include(po => po.ProductPropertyOptionForProducts)
.ThenInclude(o => o.ProductPropertyOption)
.ThenInclude(p => p.Property)
.Where(g => g.ProductGroupId == groupId)
.OrderBy(o => o.ProductGroupSortOrder)
.ToListAsync();
列出组成员=
等待上下文
.包括(i=>i.IdentifierFormProducts)
.然后包括(i=>i.ProductIdentifier)
.Include(po=>po.ProductPropertyOptions for Products)
.然后包括(o=>o.ProductPropertyOption)
.然后包括(p=>p.Property)
.Where(g=>g.ProductGroupId==groupId)
.OrderBy(o=>o.ProductGroupSortOrder)
.ToListAsync();
。。。但结果几乎相同,而且不正确。似乎我正在获取每个产品组中所有产品的所有标识符,例如,组中的一个产品会获得一个产品编号列表,每个编号都属于组中的每个产品。我没有从两个查询中得到任何属性。在第一个查询中,您重复where子句中的连接条件……第二个查询看起来不错,但我们看不到您的模型构建。@Henkholtman可能看起来不错,但没有给出我想要的结果。每种产品都有自己的标识和其他产品的标识。没有任何产品具有任何属性。“我不知道你所说的模型构建是什么意思。”亨克奥尔特曼:我查过数据库了。首先,我创建了一个产品,并给它一个标识符。当我将产品克隆到组中时,数据库中有两个产品,每个产品都有一个标识符。但在组列表中,每个产品都显示产品1和产品2的标识符。当我再次克隆产品时,使组列表包含三个产品,每个产品显示产品1、2和3的标识符。等等尽管如此,在数据库中,每个产品只有一个标识符。如果您需要,我无法进一步帮助您