Tuesday, November 15, 2022

Linq select query ASP.NET C# - select two columns

 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=> new { PresentInstitutePostingDate = (DateTime.ParseExact(t.Field<string>("PresentInstitutePostingDate") == "" ? "01/01/1990" : t.Field<string>("PresentInstitutePostingDate"), "mm/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture)), Discipline= t.Field<string>("Discipline") });

var tt = final.ToList();

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

                {

                    DateTime.ParseExact(DataBinder.Eval(e.Row.DataItem, "PresentInstitutePostingDate").ToString(), "mm/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture).Date.ToString("D");

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

                    {

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

                    }

                }


reference:- https://www.aspsnippets.com/Articles/SELECT-multiple-columns-from-DataTable-using-LINQ-in-C-and-VBNet.aspx

No comments:

Post a Comment