0

I'm getting the data of my Datatable into a List but i need to get only the items of a matching date how can i do a Linq statement of my datatable to filter the data on the provided date? My table Structure is taken from a stored procedure

This is my Code:

System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("JoinedRecords", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            conn.Open();
            DataTable myTable = new DataTable();
            myTable.Load(cmd.ExecuteReader());
            conn.Close();

for (var rowIndex = 0; rowIndex < myTable.Rows.Count; rowIndex++)
                {
                    var row = myTable.Rows[rowIndex];
  var rowValues = new List<string>(); 
  foreach (DataColumn column in myTable.Columns)//Where column.Date.Day == 3
  {
      rowValues.Add(row[column].ToString());
  }

var jsonRow = JsonConvert.SerializeObject(rowValues);

                    // Write current row
                    reader.Write(jsonRow);

                    // Add separating comma
                    if (rowIndex != myTable.Rows.Count - 1)
                        reader.WriteLine(",");

} How can i do this? There a data in my datatable that has datetime format

Arturo Martinez
  • 389
  • 7
  • 28

2 Answers2

1

Can you try:

var myTable = new DataTable();
var rowValues = myTable.AsEnumerable().Where(r => r.Field<string>("Date") == "3").ToList();

And use a .Select() before the .ToList() to get what you want? Hard to provide more with an example of the data you have.

JEV
  • 2,494
  • 4
  • 33
  • 47
0
var rowValues = new List<String>();
var value = from r in rowValues
            where column.Date.Day == 3
            select r;
Auguste
  • 2,007
  • 2
  • 17
  • 25