当我使用MVC 4构建网络应用程序时,它有三种自行车(“山地自行车”,“旅行自行车”和“公路自行车”)。
当点击“山地自行车”时,我想获得所有型号的“山地自行车”等等。我该怎么做才能修复我的代码?
var listId = db.ProductSubcategories
.Where(pc => pc.ProductSubcategoryID == 1 || pc.ProductSubcategoryID == 2 || pc.ProductSubcategoryID == 3)
.Select(pc => pc.ProductSubcategoryID);
答案 0 :(得分:2)
如果我理解你的问题,你需要真正了解你所指的自行车的SubcategoryId是什么。
string selectedBike = "Mountain Bike";
var selectedBikeSubCategory = db.ProductSubcategories.FirstOrDefault(p => p.Name == selectedBike); // Assuming you have a name for sub categories
if (selectedBikeSubCategory != null)
{
var allMountainBikes = db.Products.Where(p => p.ProductSubcategoryID == selectedBikeSubCategoryId.ID); // will give you all products matching the selected sub category bike type
}
答案 1 :(得分:0)
var criteria = new string[] {"1","2","3"};
var listId = from x in db.ProductSubcategories where criteria.Contains(x.ProductSubcategoryID);
包含方法被认为更具可读性