Credit for that expression goes to @josecgomez.
Here’s what each part does.
Db.SubShipD.Where( r =>r.Company == callContextClient.CurrentCompany && r.PackNum == ttSubShipHRow.PackNum && r.PackLine == 1).Select( r =>r.JobNum).DefaultIfEmpty("Not Found").FirstOrDefault()
Db.SubShipD
- This is the table that holds the data you want.
.Where( ...)
- These are the conditions to select the record(s) you want.
r
- this is the reference to the row(s) that you want.
r.Company == callContextClient.CurrentCompany
- this makes sure the record belongs to the company currently in use by the BPM
r.PackNum == ttSubShipHRow.PackNum
- this makes sure the record belongs to the PackNum specified by the BPM’s temp table ttSubShipHRow (this holds a temp copy of the data being written to DB).
r.PackLine == 1
- This limits the record(s) selected to the first PackLine
.Select(r =>r.JobNum)
- the value to be returned (JobNUm of the SubShipD table record that met the “Where” conditions)
.DefaultIfEmpty("Not Found")
- Value to return if no records met the “Where”. This is optional.
.FirstOrDefault()
- Instruction to return just one record. Either the first if one is found, or a default record if none exist. I’m guessing that “defaul” part would never happen since we also specify the “DefaultIfEmpty”
An important note to point out. Since there can be many SubShipD records for a given PackNum, multiple records could exist. The r.PackLine == 1
condition was added so it only ever returns the first. This might be unnecessary, as we use the .FirstOrDefault()
instruction.
Note that if a line was added to the packer (it would be Line 1), and then a second line added (it would be Line 2), and then the first line deleted, there might be no SubShipD record for that packnum with PackLine =1.
Also if the Packer has multiple lines, it wouldn’t include the others.