Group Same Values in DataTable and Display Each Group related Data to seperate GridView using Linq.


private void Do_Work_Linq()//Group Same Values in DataTable and Display Each Group related Data to seperate GridView using Linq.
        {
            DataTable dt = Mails_List_XL(@"F:\Temp\Book1.xlsx");
            DataColumn dc = dt.Columns["ID"];

            var List_ALL = (from sel in dt.AsEnumerable() select new { ID =Convert.ToInt32(sel.Field<Double>("ID")) }).ToList();
            int Count = List_ALL.Count;

            List<List<int>> list = new List<List<int>>();//nested list
            var List_Distinct = (from tab in List_ALL
                                 group tab by
                                 new
                                 {
                                     col1 = tab.ID
                                 }
                                     into grp
                                     //orderby grp.Key.col1 ascending
                                     select new
                                     {
                                         ID = grp.Key.col1
                                     }
                            ).ToList();

            int Group_Count = List_Distinct.Count;

            for (int i = 0; i < Group_Count; i++)
            {
                List<int> list_new = new List<int>();

                for (int j = 0; j < Count; j++)
                {
                    if (List_Distinct[i].ID == List_ALL[j].ID)
                    {
                        list_new.Add(List_ALL[j].ID);
                    }
                }
                if (list_new.Count > 0)
                {
                    list.Add(list_new);
                }
            }
            foreach (List<int> dsdt in list)
            {
                GridView gv = new GridView();
                //gv.ID = dsdt.TableName;
                gv.DataSource = dsdt;
                gv.DataBind();
                mpl.Controls.Add(gv);
            }

        }

You Can Download the Working Code From here.