I’m working through the SportsStore example from Pro ASP.NET MVC 3 Framework. At the beginning of chapter 8 i’m instructed to edit my ProductController class, adding the .Where line as follows:
public ViewResult List(string category, int page = 1)
{
ProductsListViewModel viewModel = new ProductsListViewModel
{
Products = repository.Products
.Where(p => category == null || p.Category == category)
.OrderBy(p => p.ProductID)
.Skip((page - 1) * PageSize)
.Take(PageSize),
PagingInfo = new PagingInfo
{
CurrentPage = page,
ItemsPerPage = PageSize,
TotalItems = repository.Products.Count()
},
CurrentCategory = category
};
return View(viewModel);
}
When I run the code i get the following error:
Exception Details: System.Data.SqlServerCe.SqlCeException: The specified argument
value for the function is not valid. [ Argument # = 1,Name of
function(if known) = isnull ]
on the foreach line in the following code block:
@model SportsStore.WebUI.Models.ProductsListViewModel
@{
ViewBag.Title = "Products";
}
@foreach (var p in Model.Products)
{
Html.RenderPartial("ProductSummary", p);
}
<div class="pager">
@Html.PageLinks(Model.PagingInfo, x => Url.Action("List", new {page = x}))
</div>
I’ve searched a good bit and found a lot of references to this StackOverflow post in multiple places, but changing the query to
.Where(p => category == null ? true : p.Category == category)
had no effect.
Some basic information:
- This is an MVC3 project using the Razer view engine and C#.
- All of the items in my SQL Compact 4.0 database have a category.
- Commenting out the category == null bit makes the code run just fine.
- The second version I gave above is what is in the downloadable source code from their site.
It does work without the null checking, but i’m worried that if I just move on i may run into issues later on. Does anyone have any ideas as to how I can fix it?
I think the problem is that the LINQ query is being deferred until it gets to the SQL server. My guess is that the SQL compact server has an issue with checking
category == null.Try using a non-deferred LINQ method before calling the Where method. Something like