我在数据库中创建了5个表:
如有任何混淆,请附上我的ERD here ..
在这种情况下,我想要在主页上显示类别列表。当我选择其中任何一个时,它将相应地显示选项组,并且在该选项组中将显示选项。如何通过使用实体框架和LINQ查询来实现这一点?
请帮助我了解控制器逻辑。我已经映射了数据库中显示的模型。如果我使数据库关系错误,也请帮忙?
修改
我已经通过建立从类别到OptionGroups的FK关系来获得OptionGroup的列表。现在,我想以相同的操作方法获取每个选项组的列表。请使用此linq查询帮助我,然后我想要以便从该特定选项撤回产品。.请也通过linq查询帮助我。.
控制器
[HttpPost]
public ActionResult GetSubCategories(int btnValue)
{
Entities entity = new Entities();
HomeRoot root = new HomeRoot();
root.OptionGroups = entity.OptionGroups.Where(m => m.CategoryID == btnValue).ToList();
//Missing My logic Here
return View("SubCategories",root);
}
模型
public partial class ProductOption
{
public int ProductOptionID { get; set; }
public int OptionID { get; set; }
public int OptionGroupID { get; set; }
public int ProductID { get; set; }
public double OptionPriceIncrement { get; set; }
public virtual OptionGroup OptionGroup { get; set; }
public virtual Option Option { get; set; }
public virtual Product Product { get; set; }
}
public partial class Product
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Product()
{
this.OrderDetails = new HashSet<OrderDetail>();
this.ProductOptions = new HashSet<ProductOption>();
}
public int ProductID { get; set; }
public string ProductSKU { get; set; }
public string ProductName { get; set; }
public Nullable<double> ProductPrice { get; set; }
public Nullable<double> ProductWeight { get; set; }
public string ProductCartDesc { get; set; }
public string ProductShortDesc { get; set; }
public string ProductLongDesc { get; set; }
public string ProductThumb { get; set; }
public string ProductImage { get; set; }
public Nullable<int> CategoryID { get; set; }
public byte[] ProductUpdateDate { get; set; }
public Nullable<double> ProductStock { get; set; }
public Nullable<byte> ProductLive { get; set; }
public Nullable<byte> ProductUnlimited { get; set; }
public string ProductLocation { get; set; }
public string ProductColor { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<OrderDetail> OrderDetails { get; set; }
public virtual ProductCategory ProductCategory { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<ProductOption> ProductOptions { get; set; }
}
public partial class OptionGroup
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public OptionGroup()
{
this.ProductOptions = new HashSet<ProductOption>();
}
public int OptionGroupID { get; set; }
public string OptionGroupName { get; set; }
public Nullable<int> CategoryID { get; set; }
public virtual ProductCategory ProductCategory { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<ProductOption> ProductOptions { get; set; }
}
public partial class Option
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Option()
{
this.ProductOptions = new HashSet<ProductOption>();
}
public int OptionID { get; set; }
public string OptionName { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<ProductOption> ProductOptions { get; set; }
}
答案 0 :(得分:0)
在您的情况下,请这样编写以获取选定的类别选项组列表:
var SelectedCategoryOptionGroup= Context.ProductOptions
.Where(po=>po.Product.CategoryID ==SelectedCategoryID)
.Select(po=>po.OptionGroup)
.Distinct();
用Entityframework with Lazy loading
技术写。
SelectedCategoryID是您的主页选择类别ID。
否则,如果您的OptionGroup
与Category
直接相关,则必须直接将OptionGroup
引用到Category。在这种情况下,您更容易达成目标。