BPM C# Comparing Dates



I’m trying to compare two dates using C#, both of the codes below compile ok… but neither pull a result

foreach (var tvPOH in (from r in Db.POHeader where (DateTime) r["FUDate_c"] == DateTime.Today select r))
foreach (var tvPOH in (from r in Db.POHeader where DateTime.Compare((DateTime)r["FUDate_c"], DateTime.Today) == 0  select r))

I might be completely out in left field here, but don’t you have to have a modifier such as . FirstOrDefault() on your select statement?

Also possible dumb question, are you sure your UD field is in a datetime format? Only because we had a customization cowboy create a text field people were supposed to just always use yyyy-mm-dd format in.

is select r correct or is tvPOH then a complete tableset? should it be select r.PONum?

You could do it this way. The DateTime.Today would match on a short date 8/23/2020 or a datetime of 8/23/2020 12:00:00 AM

var POHeaderRows = Db.POHeader.Where(ph =>ph.Company == Session.CompanyID && ph.FUDate_c == DateTime.Today);
foreach(var POHeaderRow in POHeaderRows) {
  // Logic Here

or this way if just looking to return a true or false

bool DateMatch = Db.POHeader.Where(ph => ph.Company == Session.CompanyID 	&& ph.FUDate_c == DateTime.Today).Any();

If your field is a DateTime, that means it has a time component, which the Today may not.

You need to compare just the date portion of your FUDate_c field. Because:

08/22/2020 09:34:12 != 8/22/2020

FWIW - Somewhere between 10.1.400 and 10.2.300, some date fields in db tables were changed to DateTimes. So my BAQs that were comparing the PostDate to a user supplied date, would never match.

1 Like

Thanks @danbedwards, your first example is what I was looking for and it works, the strange thing is, in any other BPM where I’ve referred to a UD field I’ve had to use the square brackets

@SteveFossey, I’ve had a look at a few foreach statements and have not seen . FirstOrDefault() used
, and I was trying to return all fields not just the PONum

@ckrusen, thanks for the pointer, earlier I had hard coded a PO in so I could check what the date fields were returning, they were returning 23/08/2020 00:00:00 (Australian format)

When referencing columns within a temporary table, like ttPOHeader, you must use brackets around UD columns - or there are other methods. For Db tables, like Db.POHeader, there is no need for the brackets. There are many more details, but this is the simple explanation. Hope that helps.

1 Like

@danbedwards, thanks for that, I was wondering if that may be the case