Sorting an internal repeater with a LINQ query

I am trying to list a group of associations, within each association there is a “widget” that is assigned to the association. The list will include the name of the association and any widget assigned to it. The trick is that you need to sort the list of internal widgets using DisplaySequence.

EDMX model below: EDMX Model

Simplified Repeater Marking

<asp:Repeater ID="rptAssociations" runat="server">
    <ItemTemplate>      
        <div data-type="assn" id="<%# ((Association)Container.DataItem).AssociationID %>">
            <h3 style="margin-top:15px;"><%# ((Association)Container.DataItem).Acronym %> - <%# ((Association)Container.DataItem).Name %></h3> 
            <asp:Repeater runat="server" ID="rptWidgets" DataSource="<%# ((Association)Container.DataItem).AssociationWidgets %>" >
                <HeaderTemplate>                
                    <ul class="WidgetList">
                </HeaderTemplate>
                <ItemTemplate>
                    <li id="<%# ((AssociationWidget)Container.DataItem).DisplaySequence %>"><%# ((AssociationWidget)Container.DataItem).Widget.Name %></li>
                </ItemTemplate>
                <FooterTemplate>
                    </ul>
                </FooterTemplate>
            </asp:Repeater>
        </div>      
    </ItemTemplate>
</asp:Repeater>

Current request

var associations = (from a in 
    context.Associations.Include("AssociationWidgets")
                        .Include("AssociationWidgets.Widget")
    orderby a.Acronym
    select a).ToList();

rptAssociations.DataSource = associations;
rptAssociations.DataBind();

Currently, I can get the data I'm looking for with the settings that I have now. I am looking for the most efficient approach to getting the same data, however, if the widgets are listed in the correct display order.

Is there any other approach to linq query that I have to execute?

+4
source share
1 answer

():

var associations = 
    context.Associations.Select( a => 
       new {
          //... specific properties you need 
          AssociationId = a.AssociationId,
          Name = a.Name,
          ... etc
          Widgets = a.AssociateWidgets.OrderBy(aw => aw.DisplaySequence)
                                      .Select(aw => aw.Widget)
       }
    );

. ,

public class AssociationInfo
{
    public string Name {get;set;}
    ...
    public IEnumerable<Widget> Widgets{ get;set; }
}

, 'new {' 'new AssociationInfo {'

+3

All Articles