简体   繁体   English

如何将Linq中GroupBy语法的结果绑定到实体到网格视图

[英]how to bind result of GroupBy syntax in Linq to entities to grid view

i have a table in my database called "Module_Menus_Menu" and the tables data are like this : 我的数据库中有一个名为“ Module_Menus_Menu”的表,并且表数据如下:

MenuID  ParentID    Name        URL
//===================================================
   1       0        Home        /Home
   2       0        Products    /Products
   3       0        Templates   /Templates
   4       2        Prod1       /Products/Cat1
   5       2        Prod2       /Products/Cat2
   6       3        Free        /Templates/Free
   7       3        Premium     /Templates/Premium

as you see this is my menu table and home, products,templates are first level menu and Prod1 is under products and prod2 too, now i want to show them in grid view(in admin area) just for listing them and i want to use GroupBy syntax to get the data and bind grid view like this : 如您所见,这是我的菜单表和主页,产品,模板是第一级菜单,Prod1也在产品和prod2下,现在我想在网格视图中显示它们(在管理区域中)只是为了列出它们,我想使用GroupBy语法可获取数据并绑定网格视图,如下所示:

Home

Products
Prod1
Prod2

Templates
Free
Premium

i searched and found a query : 我搜索并找到一个查询:

var items = (from m in context.Module_Menus_Menu group m by m.ParentID into g select new
                    {
                       ParentID = g.Key,
                       Name = g.Select(n => n.Name),
                       MenuID = g.Select(i => i.MenuID),
                       URL = g.Select(u => u.URL)
                    }).ToList();

now when i bind it to grid view : 现在,当我将其绑定到网格视图时:

grdItems.DataSource = items;
grdItems.DataBind();

and result : 结果:

Name
System.Collections.Generic.List`1[System.String]    
System.Collections.Generic.List`1[System.String]    
System.Collections.Generic.List`1[System.String]

please help me 请帮我

When the result is Systems.Collections.Generic.List, the databinding does not know where to associate properties in the list to which column so takes each row whose type is List. 当结果为Systems.Collections.Generic.List时,数据绑定不知道将列表中的属性与哪一列相关联,因此将类型为List的每一行作为对象。 You need to set your datafield columns in your gridview. 您需要在gridview中设置数据字段列。

    <columns>
      <asp:boundfield datafield="CustomerID" headertext="Customer ID"/>
      <asp:boundfield datafield="CompanyName" headertext="Company Name"/>
      <asp:boundfield datafield="Address" headertext="Address"/>
    </columns>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM