cancel
Showing results for 
Search instead for 
Did you mean: 

SAP .NET Connector - FindBy method issue

Former Member
0 Kudos

Does anyone know if the following is a known SAP .NET Connector issue, or if a work-around exists?

Here's the issue:

When using the "FindBy" method, the SAP documentation states that it will position to the 1st occurrence meeting the criteria specified. This does not appear to be the case.

-


Example:

I have a table with 6 rows. Table is 0-based.

The key field I'm finding by is populated as follows for the table:

10

10 <---

20

20 <---

30 <---

30

If I find by "10", row 1 is returned (<---). It should return 0.

If I find by "20", row 3 is returned (<---). It should return 2.

If I find by "30", row 4 is correctly returned (<---)

-


Findings:

It appears that the results of the FindBy return one row greater than the first occurrence except if the value requested is last in the list of values. In that case, it correctly returns the first occurrence of the value.

Accepted Solutions (1)

Accepted Solutions (1)

reiner_hille-doering
Active Contributor
0 Kudos

You are right, the documentation is not correct: FindBy sorts the table and uses ArrayList.BinarySearch to find a matching item. The binary search algorithm searches for an arbitrary matching entry - not for the first one. If you get the first second or any other fitting line is simple random.

I recommend to work arround by searching backward until you find a row that doesn't match:

int index = table.FindBy(fieldname, fieldvalue);
if (index >= 0)
{
  IComparable reference = table[index];
  for (int i= index; i<=0; i--)
  { 
    IComparable row = table<i>;
    if (0 != row.Compare(reference)
      return i+1;
  }
}

Answers (0)