DataTable CreateTable() { DataTable dtable = new DataTable(); DataColumn dc; //MId CId FId PId dc = new DataColumn("MId", Type.GetType("System.Int32")); dtable.Columns.Add(dc); dc = new DataColumn("CId", Type.GetType("System.Int32")); dtable.Columns.Add(dc); dc = new DataColumn("FId", Type.GetType("System.Int32")); dtable.Columns.Add(dc); dc = new DataColumn("PId", Type.GetType("System.String")); dtable.Columns.Add(dc); return dtable; } void LinqDataTable(HttpResponse Response) { DataTable dtable = CreateTable(); for (int i = 0; i < 30; i++) { DataRow Dr; Dr = dtable.NewRow(); Dr["MId"] = i; Dr["CId"] = i + 1; Dr["FId"] = i + 2; Dr["PId"] = i%2 ==0? "A":"B"; dtable.Rows.Add(Dr); } var query = (from t in dtable.AsEnumerable() //where t.Field<string>("PId") == "A" orderby t.Field<int>("MId") group t by new { Mid = t.Field<Int32>("MId"), PId = t.Field<string>("PId") } into g //where g.Key.PId == "B" select new { MId = g.Key.Mid, PId = g.Key.PId }); var AGroup = query.Where((a => a.PId == "A")); var BGroup = query.Where((a => a.PId == "B")); var sum = query.Sum((a => a.PId)); foreach (var v in AGroup) { Response.Write("</br>" + v.PId + " " + v.MId); } foreach (var v in BGroup) { Response.Write("</br>" + v.PId + " " + v.MId); } Response.End(); }