C# Linq PartTable does not contain PartNum?

Inside a c# BPM on Erp.Part.Update / Pre-Processing,
I’m getting PartTable does not contain a definition for PartNum.

var part = (from row in Db.Part.With(LockHint.NoLock)
            where row.Company == Session.CompanyID
            && row.PartNum == ttPart.PartNum // problem is here somehow
            select row)
           .FirstOrDefault();        

Produces this error:
CS1061 ‘PartTable’ does not contain a definition for ‘PartNum’ and no accessible extension method ‘PartNum’ accepting a first argument of type ‘PartTable’ could be found (are you missing a using directive or an assembly reference?)

If I show message with ttPart.PartNum in the BPM it shows the part number so I think it should be available. What am I doing wrong?

Is this inside a foreach on ttPart? What is the rest of the code section?

That is the entire thing. Do I need more stuff around it first?

image

var ttPartRow = ttPart.Where(P => P.Added() || P.Updated()).FirstOrDefault();
if(ttPartRow != null)
{ 
var part = Db.Part.With(LockHint.NoLock).Where(P => P.Company == CompanyID && P.PartNum == ttPartRow.PartNum).FirstOrDefault();
}
2 Likes

Thank you. I just assumed that the ttPartRow dataset was always available but I guess you have to declare or initialize it first.

See the following thread for a good discussion on retrieving a value from a DB table.

https://www.epiusers.help/t/simple-one-liner-c-to-retrieve-data-from-a-table/44627/2

1 Like

Also note that if you do not need the entire part record, that you should be selective about which fields to return. Being selective improves the speed… Example, if you only need the part Description and UOM, you could use THIS:

var ttPartRow = ttPart.Where(P => P.Added() || P.Updated()).FirstOrDefault();
if (ttPartRow != null) { 
    var part = Db.Part.With(LockHint.NoLock).Where(P => 
    P.Company == CompanyID && 
    P.PartNum == ttPartRow.PartNum).Select(P=>new {P.PartDescrition,P.IUOM}).FirstOrDefault(); }

if you only need one value, such as the description, you can simplify this even further, and return that one value into a string. In this case, if the part is not found, it will put “NotFound” in the description:

var ttPartRow = ttPart.Where(P => P.Added() || P.Updated()).FirstOrDefault();
if (ttPartRow != null) { 
    string partDescription = Db.Part.With(LockHint.NoLock).Where(P => 
    P.Company == CompanyID && 
    P.PartNum == ttPartRow.PartNum).Select(P=>P.PartDescrition}).FirstOrDefault() ?? "Not Found"; }

How would you extract the PartDesc and UOM from the var part in your first example?

var ttPartRow = ttPart.Where(P => P.Added() || P.Updated()).FirstOrDefault();
if (ttPartRow != null) { 
    var part = Db.Part.With(LockHint.NoLock).Where(P => 
    P.Company == CompanyID && 
    P.PartNum == ttPartRow.PartNum).Select(P=>new {P.PartDescrition,P.IUOM}).FirstOrDefault(); }

string partDesc = /* what goes here?  "part.PartDescription" ??*/
string partUOM = /* what goes here?  "part.IUOM" ?? */

@ckrusen Yes, part.?? You can check with part. and doing a control + space