我在我的数据库类别和子类别和产品三个表:
种类有很多子目录子目录有许多产品
所以我想加盟此三个表来获取数据的每个产品在这样的一个视图中显示它:
public ActionResult Index()
{
var memberId = WebSecurity.CurrentUserId;
var productsCompany = db.Products
.Join(db.SubCategorys, p => p.SubCategoryID, subcat => subcat.SubCategoryID,
(p, subcat) => new { p = p, subcat = subcat })
.Join(db.Categorys, temp0 => temp0.subcat.CategoryID, cat => cat.CategoryID,
(temp0, cat) => new { temp0 = temp0, cat = cat })
.Join(db.Sizes, temp1 => temp1.temp0.p.SizeID, s => s.SizeID,
(temp1, s) => new { temp1 = temp1, s = s })
.Join(db.Colors, temp2 => temp2.temp1.temp0.p.ColorID, c => c.ColorID,
(temp2, c) => new { temp2 = temp2, c = c })
.Join(db.Stores, temp3 => temp3.temp2.temp1.temp0.p.StoreID, st => st.StoreID,
(temp3, st) => new { temp3 = temp3, st = st })
.Join(db.Companyies, temp4 => temp4.st.CompanyID, camp => camp.CompanyID,
(temp4, camp) => new { temp4 = temp4, camp = camp })
.Where(temp5 => (temp5.camp.UserID == memberId))
.Select(temp5 => new
{
CategoryName = temp5.temp4.temp3.temp2.temp1.cat.CategoryName,
SubCategoryName = temp5.temp4.temp3.temp2.temp1.temp0.subcat.SubCategoryName,
ProductImageURL = temp5.temp4.temp3.temp2.temp1.temp0.p.ProductImageURL,
ProductName = temp5.temp4.temp3.temp2.temp1.temp0.p.ProductName,
Price = temp5.temp4.temp3.temp2.temp1.temp0.p.Price,
SizeName = temp5.temp4.temp3.temp2.s.SizeName,
ColorName = temp5.temp4.temp3.c.ColorName,
Quantity = temp5.temp4.temp3.temp2.temp1.temp0.p.Quantity,
Sales = temp5.temp4.temp3.temp2.temp1.temp0.p.Sales,
Discount = temp5.temp4.temp3.temp2.temp1.temp0.p.Discount,
StoreName = temp5.temp4.st.StoreName,
CompanyName = temp5.camp.CompanyName
}).ToList();
return View(productsCompany);
}
但这种方式需要时间,所以我”尝试这样的一种方式来获得数据:
public ActionResult Index()
{
var memberId = WebSecurity.CurrentUserId;
var productsCompany = db.Products.Include(p => p.Color).Include(p => p.Size).Include(p => p.Store).Include(p => p.SubCategory);
return View(productsCompany.ToList());
}
但我无法弄清楚如何从这样的threed表类别获得数据,这仅用于显示在该指数中的数据查看有关如何从这个三个表,并感谢所有帮助创建一个新的产品任何想法
更新我的课是:
产品类别
public class Product
{
[ScaffoldColumn(false)]
public int ProductID { get; set; }
[DisplayName("Image URL")]
//[DataType(DataType.Url)]
public string ProductImageURL { get; set; }
[Required(ErrorMessage = "Product Name is required")]
[DisplayName("Product Name")]
[StringLength(40)]
public string ProductName { get; set; }
[Required(ErrorMessage = "Price is required")]
[DisplayName("Product Price")]
[DataType(DataType.Currency)]
[Range(1, 5000.00, ErrorMessage = "Price must be between 1 SP and 5000.00 SP")]
public decimal Price { get; set; }
[Required(ErrorMessage = "Quantity is required")]
[DisplayName("Product Quantity")]
public int Quantity { get; set; }
[DisplayName("Sales Amount")]
[Range(0, 100, ErrorMessage = "sale Prsent must be between 1 and 100")]
public int Sales { get; set; }
//Exclude
public decimal Discount { get; set; }
[Required(ErrorMessage = "Color is required")]
[DisplayName("Color")]
public int ColorID { get; set; }
public virtual Color Color { get; set; }
[Required(ErrorMessage = "Size is required")]
[DisplayName("Size Type")]
public int SizeID { get; set; }
public virtual Size Size { get; set; }
[Required(ErrorMessage = "Store is required")]
[DisplayName("Store")]
public int StoreID { get; set; }
public virtual Store Store { get; set; }
[Required(ErrorMessage = "Category Type is required")]
[DisplayName("Sub Category Type")]
public int SubCategoryID { get; set; }
public virtual SubCategory SubCategory { get; set; }
}
子目录类:
public class SubCategory
{
[ScaffoldColumn(false)]
public int SubCategoryID { get; set; }
[Required(ErrorMessage = "Category Type is required")]
[DisplayName("Category Type")]
[StringLength(40)]
public string SubCategoryName { get; set; }
[Required(ErrorMessage = "Category is required")]
[DisplayName("Category")]
public int CategoryID { get; set; }
public virtual Category Category { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
Category类:
public class Category
{
[ScaffoldColumn(false)]
public int CategoryID { get; set; }
[Required(ErrorMessage = "Category Name is required")]
[DisplayName("Category Name")]
[StringLength(50)]
public string CategoryName { get; set; }
public virtual ICollection<SubCategory> SubCategorys { get; set; }
}