我可以在 LinQ 中定义默认排序顺序吗

Can I define Default Sort order in LinQ(我可以在 LinQ 中定义默认排序顺序吗)
本文介绍了我可以在 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?

伪代码(已更新解决方案):

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>

也许该方法看似简单,但我希望内部 Products 按字段排序.如果我没记错的话,我看不到以声明方式执行此操作的方法,因为 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) %>

希望能帮到别人!

推荐答案

我的假设是 Products 是一个 IEnumerable(或 IQueryable).如果是这样,为什么不将 OrderBy 方法添加到评估中,如下所示:

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 中定义默认排序顺序吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Wrap a delegate in an IEqualityComparer(在 IEqualityComparer 中包装委托)
C#: Altering values for every item in an array(C#:更改数组中每个项目的值)
What is the difference between Funclt;string,stringgt; and delegate?(Funclt;string,stringgt; 有什么区别?和委托?)
Cannot convert lambda expression to type #39;object#39; because it is not a delegate type(无法将 lambda 表达式转换为类型“对象,因为它不是委托类型)
What is the difference between lt;%: and lt;%= in ASP.NET MVC?(ASP.NET MVC 中的 lt;%: 和 lt;%= 有什么区别?)
linq query for tag system - search for multiple tags(标签系统的 linq 查询 - 搜索多个标签)