Good morning, I’ve got two lists that I need to merge together and create a data table from, I’ve got the following block of code:
private static DataTable GetDataTable(IList<DataValue> listOneDataValues,
IList<DataValue> listTwoDataValues)
{
var dataTable = new DataTable();
dataTable.Columns.Add("ColumnFromListOne");
dataTable.Columns.Add("ColumnFromListTwo");
dataTable.Columns.Add("TimeStamp");
//Group the lists together
var query = (from listOne in listOneDataValues
from listTwo in listTwoDataValues
let columnFromListOne= listOne.DoubleValue
let columnFromListTwo= listTwo.DoubleValue
let timestamp = listOne.TimeStamp
where listOne.TimeStamp == listTwo.TimeStamp
select new {ColumnFromListTwo = columnFromListOne, ColumnFromListOne = columnFromListTwo, Timestamp = timestamp});
foreach(var q in query)
dataTable.Rows.Add(q.ColumnFromListOne, q.ColumnFromListTwo, q.TimeStamp);
return dataTable;
}
The problem is the two lists contain timestamps that are off by a matter of seconds, and they don’t align at all, so my end result ends up with one or zero records in the datatable, even though each list contains 200+ records. I’m pretty bad with LINQ and would appreciate a point in the right direction. I guess I need to interpolate the timestamps before grouping, but I would like to know the best practice pattern for doing something like this.
You need to decide what an appropriate threshold is for calling two time stamps “equal” – knowing that too large a threshold will give you false positives and too small a threshold will prevent some records from joining.
From there just change your query to