PROBLEM:
The SQL statement returns a correct number of bill-error but the llblgen code segment does not. It seemed to returns all Bill-Error instead of the number of bill-error for a particular batch.
The SQL statement is:
SELECT COUNT(distinct Claim_Bill_Error.Claim_Bill_Id ) FROM Claim_Bill_Error
INNER JOIN Claim_Bill ON Claim_Bill_Error.Claim_Bill_Id = Claim_Bill.Id
INNER JOIN Batch on Batch.Id = Claim_Bill.Batch_Id
WHERE Batch.BatchNumber = '002676'
For above query (you only want the result of one Batch, so the number of results will be one) you simply should use:
PredicateExpression filter = new PredicateExpression(BatchFields.BatchId == "002676");
IRelationCollection relations = new RelationCollection();
relations.Add(BatchEntity.Relations.ClaimBillEntityUsingBatchId);
relations.Add(ClaimBillEntity.Relations.ClaimBillErrorEntityUsingClaimBillId);
int numberOfErros = 0;
using (DataAccessAdapter adapter = new DataAccessAdapter())
{
numberOfErros = (int) adapter.GetScalar(ClaimBillErrorFields.ClaimBillId, null, AggregateFunction.Count, filter, null, relations);
}
If you want the NumberOfErrors of the total collection of batch, your query would look like:
SELECT Batch.BatchId, COUNT(Claim_Bill_Error.Claim_Bill_Id)
FROM Claim_Bill_Error INNER JOIN Claim_Bill ON Claim_Bill_Error.Claim_Bill_Id = Claim_Bill.Id
INNER JOIN Batch on Batch.Id = Claim_Bill.Batch_Id
GROUP BY Batch.BatchId
This in LLBLGenPro should look like (you don't need scalarQueryExpression in this case):
ResultsetFields fields = new ResultsetFields(2);
fields.DefineField(BatchFields.BatchId, 0);
fields.DefineField(ClaimBillErrorFields.ClaimBillId, 1, AggregateFunction.Count);
GroupByCollection groupBy = new GroupByCollection();
groupBy.Add(fields[0]);
IRelationPredicateBucket filter = new RelationPredicateBucket();
filter.relations.Add(BatchEntity.Relations.ClaimBillEntityUsingBatchId);
filter.relations.Add(ClaimBillEntity.Relations.ClaimBillErrorEntityUsingClaimBillId);
DataTable resulset = new DataTable();
using (DataAccessAdapter adapter = new DataAccessAdapter())
{
adapter.FetchTypedList(fields, resulset, filter, 0, null, false, groupBy);
}
bunzee wrote:
Can you post the LLBLGenPro SQL Generated code?
What generated code? This code is handwritten by myself.
I was talking about LLBLGenPro tracing, sorry if that wasn't obvious (See LLBLGenPro Help - Using generated code - Troubleshooting and debugging)
I hope this was helpful.