我可以在LINQ定义默认的排​​序顺序 [英] Can I define Default Sort order in LinQ

查看:147
本文介绍了我可以在LINQ定义默认的排​​序顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个嵌套的ListView,和我打电话在LINQ相关的表,我怎么对它进行排序,而不是诉诸于母公司ItemDataBound事件?

If I have a nested ListView, and I'm calling a related table in LinQ, how do I sort it, without resorting to the ItemDataBound event of the parent?

伪code(更新了溶液):

Pseudo Code (UPDATED WITH SOLUTION):

<asp:ListView ID="lv" runat="server" OnItemDataBound="lv_ItemDataBound" >
   <LayoutTemplate>
      <!-- Product Category Stuff --> 
      <asp:PlaceHolder Id="itemPlaceholder" runat="server"></asp:PlaceHolder>
   </LayoutTemplate>

   <ItemTemplate>
      <asp:ListView ID="lvInner" runat="server" DataSource='<%# <%# ((Category)Container.DataItem).Products.OrderBy(p => p.Description) %> %>'>
         <LayoutTemplate>
            <ul>
               <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
            </ul>
         </LayoutTemplate>
         <ItemTemplate>
            <li>Item Stuff</li>
         </ItemTemplate>
      </asp:ListView>
   </ItemTemplate>
</asp:ListView>

也许方法看似简单,但我想内的产品以被一个字段排序。我看不到的方式来声明做得一样LinQ在创建上飞这个查询中,如果我没有记错的话,并没有做排序。

Perhaps the method is deceptively simple, but I want the inner Products to be sorted by a field. I can't see a way to do it declaratively as LinQ creates this Query on the fly, if I'm not mistaken, and doesn't do sorting.

有什么想法?

更新

更新了示例为以下内容:

Updated the Example to the following:

<%# ((Category)Container.DataItem).Products.OrderBy(p => p.Description) %>

希望它可以帮助别人!

Hope it helps someone else!

推荐答案

我的假设是,产品是的IEnumerable&LT;产品&GT; (或IQueryable的)。如果是这样的话,为什么不添加排序依据的方法来评价,像这样:

My assumption is that Products is an IEnumerable<Product> (or IQueryable). If that is the case, why not just add the OrderBy method to the evaluation, like so:

<%# Eval("Products.OrderBy(p => p.FieldToSortOn)") %>

这篇关于我可以在LINQ定义默认的排​​序顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆