How to return a single row

I am doing an efx and want to return a single row from a tableset. I believe I need to create the object as JobProdRow instead of var, but I am blanking on how to do it. Any help is appreciated.

var so = jeTableset.JobProd.Where(je=>je.Company == "X" && je.JobNum == jobNum)
                            .Select(je=>je).SingleOrDefault();
1 Like

What is going to consume this row? Do you want it to stay as a JobProdRow or will you need it as JSON?

Staying within the efx, so need it to be JobProdRow. I believe that would allow a single row to return? I believe the var is keeping it as a table?

That’s fine then. You can also leave out the .Select and just go:

var so = jeTableset.JobProd.Where(je=>je.Company == "X" && je.JobNum == jobNum).SingleOrDefault();
1 Like

No, it will be a row.

If you want, make it:

JobProdRow so = jeTableset.JobProd.Where(je=>je.Company == "X" && je.JobNum == jobNum).SingleOrDefault();

and see for yourself.

I keep getting this error.

The table jeTableset.JobProd has more than one record

Without the rest of the code I’m lost.

var so = jeTableset.JobProd.Where(je=>je.Company == "GTRM" && je.JobNum == jobNum).SingleOrDefault();

var rec = soTableset.OrderDtl.Where(s=>s.Company == so.Company &&
                                    s.OrderNum == so.OrderNum &&
                                    s.OrderLine == so.OrderLine)
                             .Select(s=>s).SingleOrDefault();

List<string> label = new List<string>();
string location = XX;

string partNum = rec.PartNum.Replace(",","");
string partDesc = rec.LineDesc.Replace(",","");
string poNum = soTableset.OrderHed.Where(s=>s.Company == so.Company && s.OrderNum == so.OrderNum)
                                  .Select(s=>s.PONum).FirstOrDefault();
string poLine = rec.POLine;

label.Add("Axcelis," + lblQty.ToString() + "," + partNum.ToString() + "," + partDesc.ToString() + "," + poNum.ToString() + "," + poLine.ToString());

string fileName = CompanyID.ToString() + "AxLabel" + DateTime.Now.Millisecond.ToString() + ".txt";
string pathString = location.ToString() + fileName.ToString();
System.IO.File.WriteAllLines(pathString, label);

Wedding Crashers Movie GIF by filmeditor

One sec, had a contractor show up.

SingleOrDefault() will throw an exception if there is more than one elements in a collection or for the specified condition.

Try FirstOrDefault.

Same error

You change it in both places?

Yes

Full efx. First widget is Erp.JobEntry.GetByID. Second widget is Erp.SalesOrder.GetByID. Condition checks the Customer ID. Then my Custom Code from above. The last two are success and failure for REST call.

Disconnect the custom code block and see if it still errors out.

Shoot, it is the widget causing the error. Here I thought it was my code. Is there a way to return a single in an expression?

I honestly don’t know.

Since you got code already you could just do it all there ?

I cleaned your original a bit if you don’t mind.

  var so = jeTableset.JobProd.Where(je =>
      je.Company == "GTRM" &&
      je.JobNum == jobNum).FirstOrDefault();

  var rec = soTableset.OrderDtl.Where(s =>
      s.Company   == so.Company &&
      s.OrderNum  == so.OrderNum &&
      s.OrderLine == so.OrderLine).FirstOrDefault();


  List<string> label = new List<string>();
  string location = XX;
  
  string partNum = rec.PartNum.Replace(",","");
  string partDesc = rec.LineDesc.Replace(",","");
  string poNum = soTableset.OrderHed.Where(s=>s.Company == so.Company && s.OrderNum == so.OrderNum).FirstOrDefault().PONum;
  
  string poLine = rec.POLine;
  
  label.Add("Axcelis," + lblQty.ToString() + "," + partNum + "," + partDesc + "," + poNum + "," + poLine);
  
  string fileName = CompanyID + "AxLabel" + DateTime.Now.Millisecond.ToString() + ".txt";
  string pathString = location + fileName;
  System.IO.File.WriteAllLines(pathString, label);

I try to avoid coding if I have to, but looks like I should use a code block to select a single OrderNum.

I never mind when someone who knows what they are doing fixes my stuff. :grinning:

1 Like

The expression only has to be valid C#. You can do a linq with FirstOrDefault from Db or your tableset

1 Like

Can you avoid the linq and just use a BAQ and the DynamicQuery business object?