我正在尝试列出具有共同ProductGroupId
的产品列表。每个产品都有任意数量的标识符(产品编号,EAN,ISBN等)和任意数量的属性(颜色,高度,大小等)。每个属性都有任意数量的可选选项(如“红色”,“绿色”等颜色)。
产品存储在Products
。
标识符标签(“EAN”,“ISBN”等)存储在ProductIdentifiers
中,产品的标识符值存储在IdentifiersForProducts
中。例如:EAN(标签):7044304034373(值)。
属性标签(例如“颜色”)存储在ProductProperties
中,属性的可用选项(“红色”,“绿色”等)存储在ProductPropertyOptions
中,产品的特定属性的选定选项ID存储在PropertyOptionsForProducts
。
以下是模型:
产品
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
}
标识符
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; }
}
我目前的弗兰肯斯坦的怪物查询看起来像这样:
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<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();
...但结果几乎相同,而且不正确。我似乎在每个产品上获得该组中所有产品的所有标识符,因此例如组中的一个产品获得产品编号列表,每个产品编号都属于该组中的每个产品。而且我没有从两个查询中获得任何属性。