问题
I've only been using asp.net for about a month now so I hope my vernacular makes sense. I would much rather prefer to use MVC but this particular view page seemingly has to be using aspx and code-behind files as it needs to be "included" in a flat-file CMS.
I have 3 nested ListViews. The first is populated (databound) from my code-behind file using LINQ and works great, gets a list of years. Then for the second ListView I call a function GetRecordsFromYear which gets all the records, again using LINQ in the code-behind.
<asp:ListView ID="lv2" runat="server" DataSource='<%# GetRecordsFromYear(Convert.ToInt32(Container.DataItem)) %>'>
Next, for each Record there is a navigation property (one-to-many relationship with another table) with Degrees. The Degrees table has a column named "gradyear" which is what I want to sort this last ListView by. I can output this fine by doing something like:
<asp:ListView ID="lv3" runat="server" DataSource='<%# Eval("Degrees") %>'>
Everything works perfectly except I can't figure out how to sort this final ListView. I can't use LINQ because I never really retrieved this data, it's a navigation property of the first table Records. I guess I could probably just use another function to retrieve the data but it seems silly to have to keep querying the database all the time when I have the navigation property available to me. I don't want a button to sort it, just have it sorted by default.
回答1:
Have you tried:
<asp:ListView ID="lv3" runat="server" DataSource='<%# From o in Eval("Degrees") order by o.gradyear %>'>
Option 2: If Eval("Degrees") is an ienumerable you can do something like
<asp:ListView runat="server" ID="lstInner" DataSource='<%# GetDegrees(Eval("Degrees")) %>'>
vb side:
Public Function GetDegrees(degrees As IEnumerable) As IEnumerable
Return From o In degrees Order By o.gradyear
End Function
来源:https://stackoverflow.com/questions/9925697/asp-net-sorting-an-inner-listview