I have a UD Field on the Part Bin table which is date stamped when an entry is made to the PartBin table.
I am wanting to update the Material Queue record (Auto Replen Trans Type) with the Bin Number which has the oldest date from the PartBin.
I have created the main bulk of the code but i cannot sort through a datetime like i would be able to an integer.
Is there anyone who could help me?
My current code is below as though I would sieve through an integer field.
Also i am yet to figure out once i have the oldest record how to set the MtlQueue.FromBinNum as the PartBin.BinNum
int x =0;
var ttMtlQueue_xRow = (from row in ttMtlQueue select row).FirstOrDefault();
if (ttMtlQueue_xRow != null)
{
foreach(var PartBin_Row in (from row in Db.PartBin.With(LockHint.UpdLock) where row.Company == ttMtlQueue_xRow.Company && row.PartNum == ttMtlQueue_xRow.PartNum select row))
{
if( x < PartBin_Row[“lastreceiveddate”])
{
x = PartBin_Row[“lastreceiveddate”];
}
}
This LOOKS like you are trying to update the BINNUM field with the DATE found in the PartBin.LastReceivedDate field… that is confusing to me.
Did you know you can OrderBy in your linq? then you don’t need to do your foreach loop… the first one retrieved will be the record you want… ANYWAY… what i THINK you want is the following which will return the BIN that has the oldest last received date. If there
But the answer is… “OrderBy” is always ascending or Oldest date first. If you wanted the NEWEST received date, you would change the query to have “.OrderByDescending”:
It may be working however I have some ‘lastreceiveddate’ with null values and I believe this will show Null values first. Can these null values be ignored?
I have added the following above the first order by which did not end up working.
Right this code works perfectly as i want it.
Thank you guys for the help and that website is pretty great so i will be using that in future.
Working code below.
@ckrusen I believe the two LINQ syntaxes are Comprehension and Lambda. Lambda can be more difficult to read at first and Comprehension is easier to read when grouping / joining (more like SQL). I believe you can mix both syntaxes
//Lambda
var i = Db.Customer.With(LockHint.NoLock).
Where(p=>p.Company==callContextClient.CurrentCompany);
//Comprehension
var i = from cust in Db.Customer.With(LockHint.NoLock)
where cust.Company == callContextClient.Company
select cust;