显示一个IGrouping<>嵌套列表视图 [英] Displaying an IGrouping<> with nested ListViews

查看:143
本文介绍了显示一个IGrouping<>嵌套列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从我的数据访问层,由widget.Manufacturer分组,检索一组小部件在一组嵌套ASP.NET列表视图来显示。

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.

问题是,(据我可以告诉)嵌套的ListView方法需要我使用它之前塑造的数据,我想不出采取最好的办法。我已经能够拿出迄今最好是把一个LINQ查询我的数据访问层,像这样:

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:

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 };

当然,匿名类型不能被传来传去,这样是行不通的。定义自定义级封装的数据好像不对的路要走。有一些方法可以让我对事物的ASP.NET端进行分组?我使用ObjectDataSources访问DAL。

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.

更新:好吧,我不会再创建一个匿名的类型,而不是我的DAL传递一个的IEnumerable< IGrouping<制造商,窗口小部件>> 来的ASP.NET页面,但如何我可以在列表视图中使用它?我需要作出以下HTML(或某事pretty很像吧)

Updated: OK, I'm not creating an anonymous type anymore, and instead my DAL passes an IEnumerable<IGrouping<Manufacturer, Widget>> 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)

<ul>
  <li>Foo Corp.
    <ol>
      <li>Baz</li>
      <li>Quux</li>
    </ol>
  </li>
  <li>Bar Corp.
    <ol>
      <li>Thinger</li>
      <li>Whatsit</li>
    </ol>
  </li>
</ul>

我本来像这样一个ListView内的一个ListView:

Originally, I had a ListView within a ListView like so:

<asp:ListView ID="ManufacturerListView">
    <LayoutTemplate>
        <ul>
            <asp:Placeholder ID="itemPlaceholder" runat="server" />
    	</ul>
    </LayoutTemplate>
    <ItemTemplate>
        <li><asp:Label Text='<%# Eval("Manufacturer.Name") %>' />
        <li>
    	<asp:ListView ID="WidgetsListView" runat="server" DataSource='<%# Eval("Widgets") %>'>
    	    <LayoutTemplate>
    	        <ol>
    	            <asp:PlaceHolder runat="server" ID="itemPlaceholder" />
    		</ol>
    	   </LayoutTemplate>
    	   <ItemTemplate>
    	       <li><asp:Label Text='<%# Eval("Name") %>'></li>
    	   </ItemTemplate>
    	</asp:ListView>
    	</li>
    </ItemTemplate>
</asp:ListView>

请注意WidgetsListView的数据源属性怎么本身就是数据绑定。我怎么能不重塑中的数据复制此功能?

Note how the DataSource property of WidgetsListView is itself databound. How can I duplicate this functionality without reshaping the data?

这是获得一种复杂的,对不起,如果我刚刚作出了一个单独的问题,而不是。

This is getting kind of complicated, sorry if I should have just made a separate question instead.

推荐答案

好吧,我要反驳我事先声明。由于EVAL想在嵌套控制某种属性名的,我们也许应该塑造的数据。

Ok, I'm going to contradict my prior statement. Since eval wants some kind of property name in the nested control, we should probably shape that data.

public class CustomGroup<TKey, TValue>
{
  public TKey Key {get;set;}
  public IEnumerable<TValue> Values {get;set;}
}

//正是如此使用它...

// and use it thusly...

IEnumerable<CustomGroup<Manufacturer, Widget>> result =
  GetAllWidgets(widgetTypeId)
  .GroupBy(w => w.Manufacturer)
  .Select(g => new CustomGroup<Manufacturer, Widget>(){Key = g.Key, Values = g};

///以至后来...

/// and even later...

<asp:ListView ID="ManufacturerListView">
<LayoutTemplate>
    <ul>
        <asp:Placeholder ID="itemPlaceholder" runat="server" />
    </ul>
</LayoutTemplate>
<ItemTemplate>
    <li><asp:Label Text='<%# Eval("Key.Name") %>' />
    <li>
    <asp:ListView ID="WidgetsListView" runat="server" DataSource='<%# Eval("Values") %>'>
        <LayoutTemplate>
            <ol>
                <asp:PlaceHolder runat="server" ID="itemPlaceholder" />
            </ol>
       </LayoutTemplate>
       <ItemTemplate>
           <li><asp:Label Text='<%# Eval("Name") %>'></li>
       </ItemTemplate>
    </asp:ListView>
    </li>
</ItemTemplate>
</asp:ListView>

这篇关于显示一个IGrouping&LT;&GT;嵌套列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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