我的数据库类别和子类别和产品中有三个表:
类别有很多子类别 子类别有很多产品
所以我试图加入这三个表来获取每个产品的数据并将其显示在这样的视图中:
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; }
}
分类:
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; }
}
答案 0 :(得分:1)
假设您的类别,子类别和产品表与forign键相关
如果您使用 Entity Framework 访问数据库中的数据,并且 ProxyCreationEnabled 在dbContext类构造函数中设置为true(默认情况下为true),则该产品表将自动检索特定产品的相关类别和子类别数据。
Product objProduct = _datacontext.Product.where(p=> p.productId.equals(pid));
,您的Product表定义如下:
public class Product
{
public int productId {get; set;}
...
public virtual IEnumarable<Category> pCategories {get; set;}
public virtual IEnumarable<SubCategory> pSubCategories {get; set;}
}
所以现在你的 objProduct 会分别自动将相关的Category和SubCategory存储在pCategories和pSubCategories中。您可以直接访问它,无需明确加入或填充相关表格。
现在将对象作为模型传递给视图
public ActionResult Index()
{
Product objProduct = _datacontext.Product.SingleOrDefault(p=> p.productId.equals(pid));
return View(objProduct);
}
并根据要求在视图中使用模型。