65.9K
CodeProject 正在变化。 阅读更多。
Home

使用LINQ对DataTable中的记录进行分组

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (1投票)

2013 年 10 月 11 日

CPOL

4分钟阅读

viewsIcon

51692

在回答论坛问题时,我遇到了一篇关于“在数据表中分组数据”的帖子,这促使我为该帖子写下这段简短的代码片段。

在回答论坛问题时,我遇到了一篇关于“在数据表中分组数据”的帖子,这促使我为该帖子写下这段简短的代码片段。 

使用 LINQ-DataSet 扩展方法,我获取了数据表中分组的数据并在屏幕上显示它们。请参阅以下 MSDN 链接,它为您提供了 LINQ to DataSet 概念以及用于使用语言集成查询 (LINQ) 处理数据表的新扩展的概述。

http://msdn.microsoft.com/en-us/library/bb399399(v=VS.90).aspx

有了新提供的扩展,您可以像在 SQL 中一样编写查询。

请参阅以下 MSDN 使用 C#、VB.Net 的示例链接。

http://code.msdn.microsoft.com/LINQ-Sample-Queries-13a42a54

下面是使用 DataTable 进行分组并在屏幕上显示它们的示例

这是标记

<asp:Repeater ID="GridView1" runat="server" OnItemCreated="GridView1_OnRowCreated">

            <ItemTemplate>

                <span>

                    <b><%# Eval("Column1") %></b>

                </span>

                <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" BorderStyle="None">

                    <Columns>

                        <asp:BoundField DataField="Column2" HeaderText="Column2" />

                        <asp:BoundField DataField="Column3" HeaderText="Column3" />

                    </Columns>

                </asp:GridView>

                <hr />

            </ItemTemplate>

        </asp:Repeater>

这是代码

v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} Normal 0 false false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin-top:0in; mso-para-margin-right:0in; mso-para-margin-bottom:10.0pt; mso-para-margin-left:0in; line-height:115%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin;}

在回答论坛问题时,我遇到了一篇关于“在数据表中分组数据”的帖子,这促使我为该帖子写下这段简短的代码片段。 

使用 LINQ-DataSet 扩展方法,我获取了数据表中分组的数据并在屏幕上显示它们。请参阅以下 MSDN 链接,它为您提供了 LINQ to DataSet 概念以及用于使用语言集成查询 (LINQ) 处理数据表的新扩展的概述。

http://msdn.microsoft.com/en-us/library/bb399399(v=VS.90).aspx

有了新提供的扩展,您可以像在 SQL 中一样编写查询。

请参阅以下 MSDN 使用 C#、VB.Net 的示例链接。

http://code.msdn.microsoft.com/LINQ-Sample-Queries-13a42a54

下面是使用 DataTable 进行分组并在屏幕上显示它们的示例(参考:下图)。

 

public partial class Grouping : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

                GroupByDemo();

            }

        }

 

        private void GroupByDemo()

        {

            DataTable table = new DataTable();

 

            table.Columns.Add("Column1", typeof(string));

            table.Columns.Add("Column2", typeof(string));

            table.Columns.Add("Column3", typeof(string));

 

            DataRow row = table.NewRow();

            row["Column1"] = "Admin";

            row["Column2"] = "xyz";

            row["Column3"] = "cleaning";

 

            table.Rows.Add(row);

 

            row = table.NewRow();

            row["Column1"] = "Admin";

            row["Column2"] = "hjkj";

            row["Column3"] = "washing";

 

            table.Rows.Add(row);

 

            row = table.NewRow();

            row["Column1"] = "Rishi";

            row["Column2"] = "iuyiu";

            row["Column3"] = "locking";

 

            table.Rows.Add(row);

 

            row = table.NewRow();

            row["Column1"] = "Rishi";

            row["Column2"] = "uyur";

            row["Column3"] = "contracting";

 

            table.Rows.Add(row);

 

            row = table.NewRow();

            row["Column1"] = "Rishi";

            row["Column2"] = "qewq";

            row["Column3"] = "launching";

 

            table.Rows.Add(row);

 

            var groups = table.AsEnumerable();

 

            var groupList = from g in groups

                            group g by g.Field<string>("Column1") into Group1

                            select new { Column1 = Group1.Key, Properties = Group1 };

 

            List<DataClass> newList = new List<DataClass>();

 

            foreach (var item in groupList)

            {

                newList.Add(new DataClass() { Column1 = item.Column1, Properties = item.Properties.ToList<DataRow>() });

            }

 

            GridView1.DataSource = newList;

            GridView1.DataBind();

        }

 

        protected void GridView1_OnRowCreated(object sender, RepeaterItemEventArgs e)

        {

            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)

            {

                GridView GridView2 = e.Item.FindControl("GridView2") as GridView;

                if (GridView2 != null)

                {

                    DataClass dataClass = e.Item.DataItem as DataClass;

                    if (dataClass != null)

                    {

                        GridView2.DataSource = from l in dataClass.Properties

                                               select new { Column2 = l.Field<string>("Column2"), Column3 = l.Field<string>("Column3") };

                        GridView2.DataBind();

                    }

                }

            }

        }

    }

 

    public class DataClass

    {

        public string Column1 { get; set; }

        public List<DataRow> Properties { get; set; }

    }

使用 LINQ 在 DataTable 中分组记录 - CodeProject - 代码之家
© . All rights reserved.