I have an ASP.NET page that binds a listview to an IQueryable
:
<asp:ListView ID="productList" runat="server"
DataKeyNames="ProductID" GroupItemCount="6"
ItemType="products.Models.Product" SelectMethod="GetProducts">
<EmptyDataTemplate>
<div>
Sorry, no products available
</div>
</EmptyDataTemplate>
<GroupTemplate>
<ul id="da-thumbs" class="da-thumbs">
<div id="itemPlaceholderContainer" runat="server" class="text-center">
<div id="itemPlaceholder" runat="server"></div>
</div>
</ul>
</GroupTemplate>
<ItemTemplate>
<li>
<img src='/products/Catalog/Images/Thumbs/<%#:Item.ImagePath%>' class="img-responsive"/>
<div>
<span class="hidden-xs">Short Description</span>
<span class="spacer visible-xs"></span>
<a href="<%#: GetRouteUrl("ProductByNameRoute", new {productName = Item.ProductName}) %>" class="btn btn-success btn-block align-mid">View Details</a>
<a href="<%#:Item.ShortURL %>" class="btn btn-success btn-block align-mid"><%#:Item.DownloadText%></a>
</div>
</li>
</ItemTemplate>
<LayoutTemplate>
<div class="row">
<asp:PlaceHolder ID="groupPlaceholderContainer" runat="server">
<div id="groupPlaceholder" runat="server"></div>
</asp:PlaceHolder>
</div>
</LayoutTemplate>
</asp:ListView>
<asp:DataPager ID="it" runat="server" PagedControlID="productList" PageSize="6" class="btn-group pager-buttons pagination pagination-large">
<Fields>
<asp:NextPreviousPagerField ShowLastPageButton="False" ShowNextPageButton="False" ButtonType="Button" ButtonCssClass="btn" RenderNonBreakingSpacesBetweenControls="false" />
<asp:NumericPagerField ButtonType="Button" RenderNonBreakingSpacesBetweenControls="false" NumericButtonCssClass="btn" CurrentPageLabelCssClass="btn disabled" NextPreviousButtonCssClass="btn" />
<asp:NextPreviousPagerField ShowFirstPageButton="False" ShowPreviousPageButton="False" ButtonType="Button" ButtonCssClass="btn" RenderNonBreakingSpacesBetweenControls="false" />
</Fields>
</asp:DataPager>
GetProducts()
is defined as follows:
public IQueryable<Product> GetProducts(
[QueryString("id")] int? categoryId,
[RouteData] string categoryName)
{
var _db = new products.Models.ProductContext();
IQueryable<Product> query = _db.Products;
if (categoryId.HasValue && categoryId > 0)
{
query = query.Where(p => p.CategoryID == categoryId);
}
if (!String.IsNullOrEmpty(categoryName))
{
query = query.Where(p =>
String.Compare(p.Category.CategoryName,
categoryName) == 0);
}
var random = new Random();
query = query.OrderBy(product => random.Next()).Where (p => p.Active == 1);
return query;
}
The issue is Random()
does not work here. When I run my app, I get an error
LINQ to Entities does not recognize the method 'Int32 Next()' method, and this method cannot be translated into a store expression.
Probably one solution is to change it to IEnumerable
but then I need IQueryable
for paging.
Update: After using Taher's solution, this is the internal query I get:
{SELECT
[Project1].[ProductID] AS [ProductID],
[Project1].[ProductName] AS [ProductName],
[Project1].[Description] AS [Description],
[Project1].[ImagePath] AS [ImagePath],
[Project1].[DownloadText] AS [DownloadText],
[Project1].[DownloadURL] AS [DownloadURL],
[Project1].[ShortURL] AS [ShortURL],
[Project1].[UnitPrice] AS [UnitPrice],
[Project1].[CategoryID] AS [CategoryID],
[Project1].[Active] AS [Active],
[Project1].[ShortDescription] AS [ShortDescription],
[Project1].[Priority] AS [Priority]
FROM ( SELECT
RAND() AS [C1],
[Extent1].[ProductID] AS [ProductID],
[Extent1].[ProductName] AS [ProductName],
[Extent1].[Description] AS [Description],
[Extent1].[ImagePath] AS [ImagePath],
[Extent1].[DownloadText] AS [DownloadText],
[Extent1].[DownloadURL] AS [DownloadURL],
[Extent1].[ShortURL] AS [ShortURL],
[Extent1].[UnitPrice] AS [UnitPrice],
[Extent1].[CategoryID] AS [CategoryID],
[Extent1].[Active] AS [Active],
[Extent1].[ShortDescription] AS [ShortDescription],
[Extent1].[Priority] AS [Priority]
FROM [dbo].[Products] AS [Extent1]
WHERE 1 = [Extent1].[Active]
) AS [Project1]
ORDER BY [Project1].[C1] ASC}
Update: Here's the modified code using Taher's and JCL's suggestions:
if (!String.IsNullOrEmpty(categoryName))
{
Session["rand"] = null;
query = query.Where(p =>
String.Compare(p.Category.CategoryName,
categoryName) == 0);
}
var seed = 0;
if (Session["rand"] == null)
{
seed = (int)DateTime.Now.Ticks % 9395713;//a random number
Session["rand"] = seed;
}
var readSeed = (int)Session["rand"];
query = query
.OrderBy(product => SqlFunctions.Rand(product.ProductID * readSeed % 577317));
return query;