How to Get the RowCount In BPM?

Dear Team,

I’m trying to get the RowCount from the table in BPM using the method like Pre-Processing & Post-Processing. But i’m not able to get the rowcount. I used the below code. Kindly help on this were i made a mistake in my BPM code.

BPM Code:

tmpPartNo = callContextBpmData.Character02;

//CallContext.Current.ExceptionManager.AddBLException(tmpPartNo.ToString());
InfoMessage.Publish(tmpPartNo.ToString());
InfoMessage.Publish("Test  222222222222222222");

var dbPart_Row = (from P in Db.Part where (P.Company == Session.CompanyID && P.PartNum == tmpPartNo) select P).FirstOrDefault();
if(dbPart_Row != null )
{
  string tmpdbPartNo = Convert.ToString(dbPart_Row["PartNum"]);
  
  InfoMessage.Publish(tmpdbPartNo.ToString());
  
  var dbPartRow = Db.PartRev.AsEnumerable().Where(x => x.PartNum == tmpdbPartNo);
  
  //CallContext.Current.ExceptionManager.AddBLException(dbPartRow.ToString());
  
  if(dbPartRow.Count() < 0)
  {
    InfoMessage.Publish("Test  8776765656");
    dbPart_Row.InActive = true;
  } 
}

Try

dbPartRow.Count
1 Like

You dont have to pre-select… you can select and count with one command… try either of these (your choice on formatting). If you dont need the actual data being selected, it is better NOT to select it into a local table, and simply return the count.

int PartCount = (from P in Db.Part where (P.Company == Session.CompanyID && P.PartNum == tmpPartNo) select P).Count();

(I prefer this style:)

int PartCount = Db.Part.Where(P.Company == Session.CompanyID && P.PartNum == tmpPartNo).Count();

OH… Also… if you dont really need the COUNT of the parts, but want to know “Does the part exist”, it is MUCH more efficient to simply ask if there is ANY record… “Any” queries return a “true/false” value. This would be the best. Why? once SQL finds the first one, it stops looking… but when you count, SQL has to find all the records that match so that it can count them.

bool PartFound = Db.Part.Where(P.Company == Session.CompanyID && P.PartNum == tmpPartNo).Any();

It is interesting that you are trying to challenge the return object in two different ways. Wonder which is better.

var dbPart_Row = (from P in Db.Part where (P.Company == Session.CompanyID && P.PartNum == tmpPartNo) select P).FirstOrDefault();

if(dbPart_Row != null )

Here you are checking to see if the dataset returned is null. Is not null you continue on.

 var dbPartRow = Db.PartRev.AsEnumerable().Where(x => x.PartNum == tmpdbPartNo);
    
  if(dbPartRow.Count() < 0)

Here you are checking to see if the count of the dataset is greater than 0. Couldn’t you do the same and check for null as well?

Just curious if there is a distinct difference between the two?

Anyway if you are trying to loop through the part revs then you could try the following, maybe not.

foreach( var dbPartRow = Db.PartRev.Where(x => x.PartNum == tmpdbPartNo))
{
 // code here
}

Plus you could create the join in one call vs two. I am sure you are already thinking of doing that.

1 Like

thank you all. now its working fine.

please don’t forget to mark your question as solved.