我想呈現標籤式佈局(類別爲標籤)的搜索結果頁面。我怎樣才能確保標籤訂單是產品,文章和視頻?現在它總是按照字母順序打印(我認爲orderby方法)。我對創建自定義排序/比較相當陌生。謝謝!List <IGroup>自定義排序c#
protected void ResultsRedendering(List<Item> searchResult)
{
_searchResults = searchResult.GroupBy(i => i.TemplateName).OrderBy(p=>p.Key).ToList();
_searchResults.RemoveAll(i => i.Key != "Product" && i.Key != "Article" && i.Key != "Video");
rptResultTab.DataSource = _searchResults;
rptResultTab.DataBind();
}
這是我的項目模板如何呈現每個標籤...
<ItemTemplate>
<li><a href="#<%#((IGrouping<string , Item>)Container.DataItem).Key %>">
<%#((IGrouping<string, Item>)Container.DataItem).Key)%></a></li>
</ItemTemplate>
此外,_searchResult的類型是List<IGrouping<string,Item>>
嗨Rob,_searchResult對象的類型爲List>,並且在嘗試使用您的邏輯時抱怨它。同樣,如果我將它投入List >,它會在運行時抱怨它無法投射。 –
xoail
2011-05-10 17:44:03
你的回答給了我方向,讓它工作......謝謝你......這就是我最後做的......
_searchResults = searchResult.GroupBy(i => i.TemplateName).OrderBy(o => o.Key == "Product" ? 1 : o.Key == "Article" ? 2 : 3).ToList();
– xoail 2011-05-11 00:46:43