Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplaying an IGrouping<> with nested ListViews
    text
    copied!<p>I need to retrieve a set of Widgets from my data access layer, grouped by widget.Manufacturer, to display in a set of nested ASP.NET ListViews.</p> <p>The problem is that (as far as I can tell) the nested ListView approach requires me to shape the data before using it, and I can't figure out the best approach to take. The best I've been able to come up with so far is to put a LINQ query in my data access layer like so:</p> <pre><code>var result = from widget in GetAllWidgets(int widgetTypeID) group widget by widget.Manufacturer into groupedWidgets let widgets = from widgetGroup in groupedWidgets select widgetGroup select new { Manufacturer = groupedWidgets.Key, Widgets = widgets }; </code></pre> <p>Of course, anonymous types can't be passed around, so that doesn't work. Defining a custom class to enclose data seems like the wrong way to go. Is there some way I can perform the grouping on the ASP.NET side of things? I'm using ObjectDataSources to access the DAL.</p> <p><b>Updated</b>: OK, I'm not creating an anonymous type anymore, and instead my DAL passes an <code>IEnumerable&lt;IGrouping&lt;Manufacturer, Widget&gt;&gt;</code> to the ASP.NET page, but how can I use this in my ListViews? I need to render the following HTML (or something pretty much like it)</p> <pre><code>&lt;ul&gt; &lt;li&gt;Foo Corp. &lt;ol&gt; &lt;li&gt;Baz&lt;/li&gt; &lt;li&gt;Quux&lt;/li&gt; &lt;/ol&gt; &lt;/li&gt; &lt;li&gt;Bar Corp. &lt;ol&gt; &lt;li&gt;Thinger&lt;/li&gt; &lt;li&gt;Whatsit&lt;/li&gt; &lt;/ol&gt; &lt;/li&gt; &lt;/ul&gt; </code></pre> <p>Originally, I had a ListView within a ListView like so:</p> <pre><code>&lt;asp:ListView ID="ManufacturerListView"&gt; &lt;LayoutTemplate&gt; &lt;ul&gt; &lt;asp:Placeholder ID="itemPlaceholder" runat="server" /&gt; &lt;/ul&gt; &lt;/LayoutTemplate&gt; &lt;ItemTemplate&gt; &lt;li&gt;&lt;asp:Label Text='&lt;%# Eval("Manufacturer.Name") %&gt;' /&gt; &lt;li&gt; &lt;asp:ListView ID="WidgetsListView" runat="server" DataSource='&lt;%# Eval("Widgets") %&gt;'&gt; &lt;LayoutTemplate&gt; &lt;ol&gt; &lt;asp:PlaceHolder runat="server" ID="itemPlaceholder" /&gt; &lt;/ol&gt; &lt;/LayoutTemplate&gt; &lt;ItemTemplate&gt; &lt;li&gt;&lt;asp:Label Text='&lt;%# Eval("Name") %&gt;'&gt;&lt;/li&gt; &lt;/ItemTemplate&gt; &lt;/asp:ListView&gt; &lt;/li&gt; &lt;/ItemTemplate&gt; &lt;/asp:ListView&gt; </code></pre> <p>Note how the <code>DataSource</code> property of WidgetsListView is itself databound. How can I duplicate this functionality without reshaping the data?</p> <p>This is getting kind of complicated, sorry if I should have just made a separate question instead.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload