热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用嵌套的ListView显示IGrouping<>-DisplayinganIGrouping<>withnestedListViews

IneedtoretrieveasetofWidgetsfrommydataaccesslayer,groupedbywidget.Manufacturer,todi

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.

我需要从我的数据访问层检索一组Widgets,按widget.Manufacturer分组,以显示在一组嵌套的ASP.NET ListViews中。

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:

问题是(据我所知)嵌套的ListView方法要求我在使用之前对数据进行整形,我无法找出最佳方法。到目前为止,我能够提出的最好的方法是在我的数据访问层中放置LINQ查询,如下所示:

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

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.

当然,匿名类型不能传递,所以这不起作用。定义一个自定义类来封装数据似乎是错误的方法。有没有什么方法可以在ASP.NET方面执行分组?我正在使用ObjectDataSources来访问DAL。

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

更新:好的,我不再创建匿名类型,而是我的DAL将IEnumerable >传递给ASP.NET页面,但是我如何在ListViews中使用它?我需要渲染以下HTML(或者非常类似的东西)

  • Foo Corp.
    1. Baz
    2. Quux
  • Bar Corp.
    1. Thinger
    2. Whatsit

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

最初,我在ListView中有一个ListView,如下所示:


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

    请注意WidgetsListView的DataSource属性本身是如何数据绑定的。如何在不重塑数据的情况下复制此功能?

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

    这有点复杂,对不起,如果我应该刚刚提出一个单独的问题。

    3 个解决方案

    #1


    12  

    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.

    好吧,我要反驳我先前的陈述。由于eval在嵌套控件中需要某种属性名称,因此我们应该对该数据进行整形。

    public class CustomGroup
    {
      public TKey Key {get;set;}
      public IEnumerable Values {get;set;}
    }
    

    // and use it thusly...

    //并因此使用它......

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

    /// and even later...

    ///甚至更晚......

    
    
        
  • #2


    5  

    I've just spent quite a while on this. Eventually found the solution and it's so simple.

    我刚刚在这上花了很长时间。最终找到了解决方案并且它非常简单。


    var enumerableData = myData.Tables[0].AsEnumerable();
    var groupedData = enumerableData.GroupBy(x => x["GroupingColumn"]);
    
    myParentRepeater.DataSource = groupedData;
    myParentRepeater.DataBind();
    

    
      
        

    <%#Eval("Key") %>

    <%#((DataRow)Container.DataItem)["ChildDataColumn1"] %> <%#((DataRow)Container.DataItem)["ChildDataColumn2"] %>

    Eval("Key") returns the grouped value. When retrieving child info, Container.DataItem is of type IGrouping but you simply cast it to the correct type.

    Eval(“Key”)返回分组值。检索子信息时,Container.DataItem的类型为IGrouping,但您只需将其强制转换为正确的类型。

    Hope this helps someone else.

    希望这有助于其他人。

    #3


    3  

    When you're using Linq to group, you can get a strongly typed object without that shaping:

    当您使用Linq进行分组时,您可以获得一个强类型对象而不进行整形:

    List myInts = new List() { 1, 2, 3, 4, 5 };
    IEnumerable> myGroups = myInts.GroupBy(i => i % 2);
    foreach (IGrouping g in myGroups)
    {
      Console.WriteLine(g.Key);
      foreach (int i in g)
      {
        Console.WriteLine("  {0}", i);
      }
    }
    Console.ReadLine();
    

    In your case, you'd have:

    在你的情况下,你有:

      IEnumerable> result =
        GetAllWidgets(widgetTypeId).GroupBy(w => w.Manufacturer);
    

    This will let you return the result from the method.

    这将允许您返回方法的结果。


    推荐阅读
    • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
    • ASP.NET2.0数据教程之十四:使用FormView的模板
      本文介绍了在ASP.NET 2.0中使用FormView控件来实现自定义的显示外观,与GridView和DetailsView不同,FormView使用模板来呈现,可以实现不规则的外观呈现。同时还介绍了TemplateField的用法和FormView与DetailsView的区别。 ... [详细]
    • 本文介绍了Sencha Touch的学习使用心得,主要包括搭建项目框架的过程。作者强调了使用MVC模式的重要性,并提供了一个干净的引用示例。文章还介绍了Index.html页面的作用,以及如何通过链接样式表来改变全局风格。 ... [详细]
    • 这期内容当中小编将会给大家带来有关怎么在asp.net中获取ListView与gridview中当前行的行号,文章内容丰富且以专业的角度为大家分析和叙述, ... [详细]
    • Flutter ListView如何实现上拉加载更多下拉刷新功能
      这篇文章给大家分享的是有关FlutterListView如何实现上拉加载更多下拉刷新功能的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小 ... [详细]
    • ListView 应用5 - 数据库的增删改查及分页
      郁闷了好长一段时间,做了各种各样的修改,就是“删除”无法执行,罪魁祸首竟然是ListView控件中少设置了一个DataKeyNames属性,悲哀啊!defau ... [详细]
    • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
      本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
    • C# ListView用法详解 很完整
      一、ListView类1、常用的基本属性:(1)FullRowSelect:设置是否行选择模式。(默认为false)提示&# ... [详细]
    • 关于改变ListView选中项的状态方法思路:无论使用哪一个适配器,只需要重写他的getView()方法即可原理:getView方法是用于把创建好的view交给listView用的,所以我们可 ... [详细]
    • 具有顺序标题的ListView ... [详细]
    • Android基础入门教程——2.4.12ExpandableListView(可折叠列表)的基本使用标签(空格分隔):Android基础入门教程本节引言:本节要讲解的Adap ... [详细]
    • jQM note:开发工具的选择
      不知道其他的开发人员是否和我一样,在学习一些新的技术时,有对于开发工具的迷茫。这是一个节奏无比快的时代,对于学习周期,除了一门全新的技术或是深奥的研究,都不能太长。就如phpdevshell,在有一定 ... [详细]
    • 现在做了一个ListView,里面的值是用cursor提取数据库的,现在想点击listview以后页面进行跳转,并且在新的activity里显示被点击的listview的数据,我的listview有很 ... [详细]
    • 在WPF中,我有一个绑定到字典(ListView)的InpLangList和一个具有boolean(Ch ... [详细]
    • [java]viewplaincopyprint?ListViewlistViewgetListView(); ... [详细]
    author-avatar
    09会计系欣荣19__81_186
    这个家伙很懒,什么也没留下!
    PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
    Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有