Tuesday, November 15, 2022

Group by with max date ASP.NET C# LINQ - without model or entity

  var dt = GridView1.DataSource as DataTable;

                var a= dt.AsEnumerable().GroupBy(s => s.Field<string>("Discipline").ToString());

                

                

                //dt.DefaultView.Sort = sort;

                var final = dt.AsEnumerable().GroupBy(x => x.Field<string>("Discipline"), (key, g) => g.OrderByDescending(s => DateTime.ParseExact(s.Field<string>("PresentInstitutePostingDate") == ""?"01/01/1990": s.Field<string>("PresentInstitutePostingDate"), "mm/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture)).First()).Select(t=> DateTime.ParseExact(t.Field<string>("PresentInstitutePostingDate") == "" ? "01/01/1990" : t.Field<string>("PresentInstitutePostingDate"), "mm/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture));

                var tt = final.ToList();

                if (!string.IsNullOrEmpty(Convert.ToString(DataBinder.Eval(e.Row.DataItem, "PresentInstitutePostingDate"))))

                {

                    if (tt.Exists(o => o == DateTime.ParseExact(DataBinder.Eval(e.Row.DataItem, "PresentInstitutePostingDate").ToString(), "mm/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture)))

                    {

                        e.Row.BackColor = System.Drawing.ColorTranslator.FromHtml("#0000ff");

                    }

                }

No comments:

Post a Comment