I set up a list with 7 columns and Thousands of rows. I also have a data table with the same data (yes, redundant). What I am trying to do is get all the records related to yesterdays date.
I've started a few queries on the data table with no success. It keeps on coming up empty. Same with the list.
Below is all my code for the table/list.
DataTable dtTesting = new DataTable();
List<string>[] listA = new List<string> [8];
listA[0] = new List<string>();
listA[1] = new List<string>();
listA[2] = new List<string>();
listA[3] = new List<string>();
listA[4] = new List<string>();
listA[5] = new List<string>();
listA[6] = new List<string>();
listA[7] = new List<string>();
dtTesting.Columns.Add("Over2Minutes", typeof(string));
dtTesting.Columns.Add("AgentName", typeof(string));
dtTesting.Columns.Add("PhoneNumber", typeof(string));
dtTesting.Columns.Add("DateTime", typeof(DateTime ));
dtTesting.Columns.Add("Direction", typeof(string));
dtTesting.Columns.Add("CallTo", typeof(string));
dtTesting.Columns.Add("ManagerName", typeof(string));
dtTesting.Columns.Add("Duration", typeof(string));
// List<string> listE = new List<string>();
//List<string> listF = new List<string>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
DataRow data = dtTesting.NewRow();
data["Over2Minutes"] = values[0];
data["AgentName"] = values[1];
data["PhoneNumber"] = values[2];
data["DateTime"] = values[3];
data["Direction"] = values[4];
data["CallTo"] = values[5];
data["ManagerName"] = values[6];
data["Duration"] = values[7];
listA[0].Add(values[0]);
listA[1].Add(values[1]);
listA[2].Add(values[2]);
listA[3].Add(values[3]);
listA[4].Add(values[4]);
listA[5].Add(values[5]);
listA[6].Add(values[6]);
listA[7].Add(values[7]);
}
//start of filters
DataTable filtered = new DataTable();
DateTime yesterday = DateTime.Today.AddDays(-1);
DateTime today = DateTime.Today.AddDays(1);
//List<string> listBD = new List<string>();
// listBD.AddRange(listB);
// listBD.AddRange(listD);
//
if (cbDayList.SelectedIndex == 0)
{
try
{
var FilteredData =
}
catch
{
MessageBox.Show("No Data To Show");
}
}
If you want to filter the DataTable rows according to a particular data you use the Select method of the DataTable. But, being the FilterExpression of the Select method a string, you are forced to convert the DateTime variables to a string. And this is where the problem begin.
You cannot use the default conversion of a DateTime to a string because in case of DateTime the FilterExpression requires the date to be expressed according to the InvariantCulture format. "M/d/yyyy"
var FilteredData = dtSettings.Select("DateTime >= #" +
yesterday.ToString("M/d/yyyy") +
"# AND date <= #" +
today.ToString("M/d/yyyy") + "#");
By the way, albeit improbable this query gets also the records stored exactly at midnight. Probably you need < today.ToString("M/d/yyyy")
Another way to reach your goal is through the IEnumerable extension Where (Linq namespace)
var FilteredData = dtSettings.AsEnumerable()
.Where(x => x.Field<DateTime>("DateTime") >= yesterday &&
x.Field<DateTime>("DateTime") < today);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.