Can someone assist me with a datetime diff situation. Im trying to figure the logic out for this.
basically im trying to compare records baseoff of a datetime, from two different tables. The datetime is slightly off by plus or minus 3mins.
below you will notice a 3min difference. I want to be able to say
//plus or minus 3minutes via datetime
EList elTestInfoList = EList .Where(d => d.EL_OpTimeStamp "is plus or minus 3mins" SystemRecord.DateTime);
if both datetimes are plus or minus three minutes from each other, I want to do something. so how do you set up the logic or linq for this situation
table 1
datetime , field A, field B, etc............
row 1: 12/6/2007 7:00:00 AM,
table 2
datetime , field A, field B, etc............
Row 1: 12/6/2007 7:03:00 AM, field A , field B , etc....
Loading
David SmithPosted Oct 6, 2010, 11:27 PM
TimeSpan plus4 = new TimeSpan(0, 4, 0);
TimeSpan minus4 = new TimeSpan(0, -4, 0);
EventLogStatusInfoByOperation elTestInfoEntry = null;
try
{
elTestInfoEntry = (from x in ELStatusByOperationList
where (plus4 >= (x.EL_OpTimeStamp - SystemRecord.MDB_OpDateTime) &&
minus4<= (x.EL_OpTimeStamp - SystemRecord.MDB_OpDateTime))
select x).First
}
David SmithPosted Oct 6, 2010, 12:46 AM
Dictionary<int, TestStatusReportByOperationRecord> TestStatusByOpRecord = new Dictionary<int, TestStatusReportByOperationRecord>();
try
{
foreach (SysSummaryByOperation SystemRecord in MDBStatusByOperationList)
{
if (!TestStatusByOpRecord.ContainsKey(SystemRecord.MDB_TPC_ID))
{
TestStatusByOpRecord.Add(SystemRecord.MDB_TPC_ID, new TestStatusReportByOperationRecord());
}
//Update Record
TestStatusReportByOperationRecord testInfoRec = TestStatusByOpRecord[SystemRecord.MDB_TPC_ID];
//plus or minus 2minutes via optimestamp
//the next line below is where im trying to compare the datetime via the plus or minus 3minutes in expression, at the moment I just have something in there until I can figure out to
// add the plus or minus 3minutes.
//d.EL_OpTimeStamp and SystemRecord.MDB_OpDateTime are already coming from a list, I just want to take the SystemRecord.MDB_OpDateTime and verify if its plus or minus
// 3mins from EL_OpTimeStamp, if so then it will jump to the next couple of lines.
// I want to do the logic or linq with where you see green. you can ignore the where statement, but thats where im trying to put in the plus or minus linq
OperationList elTestInfoList = ELStatusByOperationList.Where(d => d.EL_datetime== SystemRecord.MDB_DateTime).First();
testInfoRec.A = SystemRecord.A;
testInfoRec.EL_dateTime= elTestInfoList.EL_datetime;
testInfoRec.EL_B = elTestInfoList.B;
testInfoRec.EL_C = elTestInfoList.C;
testInfoRec.MDB_Datetime = SystemRecord.MDB_OpDateTime;
}
Mike GoldPosted Oct 6, 2010, 12:05 AM
class Program
{
static void Main(string[] args)
{
var table1 = new List<DateTime> {new DateTime(2010, 10, 5, 12, 0, 0),
new DateTime(2010, 10, 7, 12, 0, 0),
new DateTime(2010, 10, 8, 12, 0, 0)};
var table2 = new List<DateTime> {new DateTime(2010, 10, 5, 12, 2, 0),
new DateTime(2010, 10, 7, 12, 4, 0),
new DateTime(2010, 10, 8, 12, 1, 0)};
var list =
table1.Select(
d1 => table2.Select(d2 => new { date1 = d1, date2 = d2 }).Where(x => (x.date1 + new TimeSpan(0, 3, 0)) > x.date2 && (x.date1 - new TimeSpan(0, 3, 0)) < x.date2)).ToList();
foreach (var item in list)
{
var innerList = item.ToList();
foreach (var date in innerList)
{
Console.Write("{0} {1}", date.date1, date.date2);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
David SmithPosted Oct 5, 2010, 11:22 PM