Friday, 12 June 2020

The source contains no DataRows OR filter datatable datetime in c#

///////////////////////////


///////// Method 1
            DataTable dt = new DataTable();

            DataTable dt2 = dt.Clone();
            var rows = dt.Select().Where(p => (Convert.ToDateTime(p["DateTime"]).Date >= System.DateTime.Now.AddDays(-50)) && (Convert.ToDateTime(p["DateTime"]) <= System.DateTime.Now.Date));

            foreach (var row in rows)
                dt2.ImportRow(row);



///////// Method 2
            DataTable dtn = new DataTable();
            dtn = dt.Clone();
            dtn = dt.Select().Where(p => (Convert.ToDateTime(p["DateTime"]).Date >= System.DateTime.Now.AddDays(-50)) && (Convert.ToDateTime(p["DateTime"]) <= System.DateTime.Now.Date)).CopyToDataTable();



 ///////// Method 3
            DataTable dt3 = new DataTable();
            dt3 = dt.Clone();
            var reRows = dt.Select().Where(m => ((DateTime)m["DateTime"]).Date >= DateTime.Now.Date);

            if (reRows.Any())
                dt3 = reRows.CopyToDataTable();


No comments:

Post a Comment

How to highlight selected text in notepad++

  –> To highlight a block of code in Notepad++, please do the following steps step-1  :- Select the required text. step-2  :- Right click...