locked
IGrouping on multiple columns RRS feed

  • Question

  • User1099429166 posted

    Dear all,

    I am doing IGrouping on a single column version right now. Below is my query

    IEnumerable<dynamic> data = dataTbl.Rows.Cast<DataRow>()
    .GroupBy<DataRow, String>(d => Convert.ToString(d["version"]))
    .Select<IGrouping<String, DataRow>, dynamic>(grp =>
    {
    return new
    {
    version = grp.Key,
    Books = grp.Select<DataRow, dynamic>(row =>
    {
    return new
    {
    message= Convert.ToString(row["data"]),
    date= row["releasedate"]

    };
    })
    };
    });

    It works fine, but I want to add another column with it . something like this d["date"].  Any help would be greatly appreciated

    Monday, November 6, 2017 8:23 PM

All replies

  • User-335504541 posted

    Hi Sam Solomon,

    Please try to use the following code:

                DataTable dataTbl = new DataTable();
                dataTbl.Columns.Add("version");
                dataTbl.Columns.Add("data");
                dataTbl.Columns.Add("releasedate");
                dataTbl.Rows.Add("v1", "d1", "r1");
                dataTbl.Rows.Add("v1", "d1", "r2");
                dataTbl.Rows.Add("v1", "d1", "r3");
                dataTbl.Rows.Add("v1", "d2", "r1");
                dataTbl.Rows.Add("v1", "d2", "r2");
                dataTbl.Rows.Add("v1", "d2", "r3");
                dataTbl.Rows.Add("v2", "d2", "r2");
                dataTbl.Rows.Add("v3", "d3", "r3");
    
    
                IEnumerable<dynamic> data = dataTbl.Rows.Cast<DataRow>()
                    .GroupBy<DataRow, String>(d => Convert.ToString(d["version"]))
                    .Select<IGrouping<String, DataRow>, dynamic>(grp =>
                    {
                        return new
                        {
                            version = grp.Key,
                            Books = grp.GroupBy<DataRow, dynamic>(g=> Convert.ToString(g["releasedate"])).Select(r=> 
                            {
                                return new
                                {
                                    releasedate = r.Key,
                                    data=r.GroupBy(x=> Convert.ToString(x["data"])).Select(Tdata=>Tdata.Key)
    
                                };
                            })
                        };
                    });

    Best Regards,

    Billy

    Tuesday, November 7, 2017 6:14 AM