Selecting the record with the latest date in BPM with C#

I am finally understanding how C# works :slight_smile: I have hit a snag. I need to get the record that has the latest PullDateTime_c. I had this set up to just check if another field was empty but our process flow is sometimes different so the field I’m checking isn’t guaranteed to be empty. I’ve tried Max and different groupings but can’t seem to get it right. How would I get the record with the latest PullDateTime_c?
Thanks
Melissa

//Link PartTran to UD10 using part and lot picking latest pulldatetime
UD10Table = (from UD10_row in Db.UD10
             where UD10_row.Company == Session.CompanyID
             && UD10_row.Character01 == tranPart
             && UD10_row.Character02 == tranLot
             //&& UD10_row.ReturnDateTime_c == null
             select UD10_row).FirstOrDefault();

Hi Melissa,

Try adding an order by PullDateTime_c descending in your query.

UD10Table = (from UD10_row in Db.UD10
             where UD10_row.Company == Session.CompanyID
             && UD10_row.Character01 == tranPart
             && UD10_row.Character02 == tranLot
             //&& UD10_row.ReturnDateTime_c == null
			 orderby UD10_row.PullDateTime_c descending  // here
             select UD10_row).FirstOrDefault();
3 Likes