[英]Linq Entity Framework where parameter on joined table
I am stuck trying to add a where parameter to my linq query on a joined table, I have a list of products displayed in ProductsListViewModel from the Products Table and need to filter by a FilterId parameter from the ProductFilters table, with SKU being the linked field, but cannot add the FilterId parameter to the where clause. 我在尝试在联接表上的linq查询中添加where参数时遇到问题,我在Products表的ProductsListViewModel中显示了一个产品列表,并且需要通过ProductFilters表中的FilterId参数进行过滤,而SKU是链接字段,但不能将FilterId参数添加到where子句。
Here is a the code I have so far for the ViewModel: 这是我到目前为止为ViewModel编写的代码:
public class ProductsListViewModel
{
public IEnumerable<Product> Products { get; set; }
public PagingInfo PagingInfo { get; set; }
public string CurrentCategory { get; set; }
public IEnumerable<ProductFilter> ProductFilters { get; set; }
}
The controller: 控制器:
public ViewResult List(string category, string filtermaterial = null, string filterlength = null, string filtername = null, string filterid = null, int page = 1)
{
ProductsListViewModel viewModel = new ProductsListViewModel
{
Products = repository.Products
.Where(p => category == null || p.Category == category)
.Where(p => filtermaterial == null || p.FilterMaterial == filtermaterial)
.Where(p => filterlength == null || p.FilterLength == filterlength)
.OrderBy(p => p.ProductID)
.Skip((page - 1) * PageSize)
.Take(PageSize),
PagingInfo = new PagingInfo
{
CurrentPage = page,
ItemsPerPage = PageSize,
TotalItems = category == null ?
repository.Products.Count() :
repository.Products.Where(e => e.Category == category).Count()
},
CurrentCategory = category
};
ViewBag.Category = category;
return View(viewModel);
}
The Product
model: Product
型号:
public class Product
{
[HiddenInput(DisplayValue = false)]
public int ProductID { get; set; }
[Required(ErrorMessage = "Please enter a product name")]
public string Name { get; set; }
[Required(ErrorMessage = "Please enter the SKU for this product")]
public string SKU { get; set; }
public string MainImage { get; set; }
[DataType(DataType.MultilineText)]
[Required(ErrorMessage = "Please enter a description")]
public string Description { get; set; }
[DataType(DataType.MultilineText)]
[Required(ErrorMessage = "Please enter product specification details")]
public string Specification { get; set; }
[Required]
[Range(0.01, double.MaxValue, ErrorMessage = "Please enter a correct price")]
public decimal Price { get; set; }
[Required(ErrorMessage = "Please enter the Product Category")]
public string Category { get; set; }
//public int CategoryID { get; set; }
[Required(ErrorMessage = "Please enter the Product Group")]
public string ProductGroup { get; set; }
[Required(ErrorMessage = "Please enter the search terms for this product")]
public string SearchTerms { get; set; }
public string FilterMaterial { get; set; }
public string FilterLength { get; set; }
public string Manufacturer { get; set; }
public string Brand { get; set; }
public string Details1 { get; set; }
public string Details2 { get; set; }
public string Details3 { get; set; }
public string Details4 { get; set; }
public string Spec1 { get; set; }
public string Spec2 { get; set; }
public string Spec3 { get; set; }
public string Spec4 { get; set; }
public virtual IQueryable<ProductFilter> ProductFilters { get; set; }
}
The ProductFilter
model: ProductFilter
模型:
public class ProductFilter
{
[Key]
public int FilterId { get; set; }
public string Filtername { get; set; }
public string CategoryGroup { get; set; }
public string SKU { get; set; }
}
Modified controller with join 带联接的修改控制器
public ViewResult List(string category, string filtermaterial = null, string filterlength = null, string filtername = null, string filterId = null, int page = 1)
{
ProductsListViewModel viewModel = new ProductsListViewModel
{
Products = repository.Products
.Join(repository.ProductFilters, prod => prod.SKU, prodfilt => prodfilt.SKU, (prod, prodfilt) => new { Prod = prod, Prodfilt = prodfilt})
.Where(p => category == null || p.Prod.Category == category)
.Where(p => filtermaterial == null || p.Prod.FilterMaterial == filtermaterial)
.Where(p => filterlength == null || p.Prod.FilterLength == filterlength)
.Where(f => filterId == null || f.Prodfilt.FilterId == filterId)
.OrderBy(p => p.Prod.ProductID)
.Skip((page - 1) * PageSize)
.Take(PageSize),
PagingInfo = new PagingInfo
{
CurrentPage = page,
ItemsPerPage = PageSize,
TotalItems = category == null ?
repository.Products.Count() :
repository.Products.Where(e => e.Category == category).Count()
},
CurrentCategory = category
};
ViewBag.Category = category;
return View(viewModel);
}
'System.Linq.IQueryable<AnonymousType#1> to System.Collections.Generic.IEnumerable<WebStore.Domain.Entities.Product>
Your Viewmodel expects a property of type IEnumerable<Product>
. 您的Viewmodel需要一个IEnumerable<Product>
类型的属性。 By joining Products with Productfilters, the returned entites are of type anonymous. 通过将Products与Productfilters结合在一起,返回的实体是匿名类型的。
to solve this conflict you need to add a strongly typed select statement: 要解决此冲突,您需要添加一个强类型的select语句:
Products = repository.Products
.Join(repository.ProductFilters, prod => prod.SKU, prodfilt => prodfilt.SKU, (prod, prodfilt) => new { Prod = prod, Prodfilt = prodfilt})
// ...
.Select( x => new Product { ... }) //add this and clarify the properties.
.Skip((page - 1) * PageSize)
.Take(PageSize)
OR you change your ViewModel 或者您更改您的ViewModel
public class ProductsListViewModel
{
public IEnumerable<ProductAndFilterVM> FilterProducts { get; set; }
public PagingInfo PagingInfo { get; set; }
public string CurrentCategory { get; set; }
public IEnumerable<ProductFilter> ProductFilters { get; set; }
}
This would require you to create another ViewModel, which would represent the joined entity. 这将需要您创建另一个ViewModel,它将代表联接的实体。 But you still need the select. 但是您仍然需要选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.