我有一個綁定列表視圖到IQueryable
一個ASP.NET頁面:使用返回隨機排一個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()
定義如下:
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;
}
的問題是Random()
不在這裏工作。當我運行我的應用程序時,出現錯誤
LINQ to Entities無法識別方法'Int32 Next()'方法,並且此方法無法轉換爲存儲表達式。
也許一個解決方案是將其更改爲IEnumerable
但後來我需要IQueryable
尋呼。
更新:使用塔希爾的解決方案後,這是內部查詢我得到:
{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}
更新:這是使用修改後的代碼塔希爾的和JCL的建議:
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;
你想基於隨機屬性對行進行排序嗎? –
目前查詢按特定順序返回行,我認爲這是基於productId - 1,2,3,4,5等。我想對結果進行隨機洗牌,以便結果不以特定順序返回。它可以是5,2,4,1,3或者下一輪運行3,5,1,4,2等等。 – CuriousDev
IWantToLearn你的問題是關於你的代碼中的錯誤,而不是隨機數字的問題! – Arash