0

I have a specific lambda what doesn't seem to want to work properly although the logic seems sound.

index = llCodeList.FindIndex(f => string.Compare(f.Threshold.ToString(), searchText, true ) >= 0);

f.Threshold is an integer value, it seems that the conversion doesn't happen and it breaks my search function. Can anyone shed some light on this?

6
  • 3
    How does it break your search? Can you show the result of some sample input? Commented May 17, 2011 at 14:07
  • 3
    A short but complete program would really help. I really doubt that you've found a bug in the C# compiler or .NET... Commented May 17, 2011 at 14:09
  • your lambda evaluates to true if f.Threshold is lexicographically equal or greater than searchText. how do f.Threshold and searchText look like? Commented May 17, 2011 at 14:12
  • 1
    SO you're trying to find the index of the first element with a threshold greater or equal to a value supplied by the user? Is that correct? Would it not be better to try and convert the searchText to an int and then compare ints? Comparing the two strings is unlikely to give you the result you want. Commented May 17, 2011 at 14:12
  • he's seeing if the search text is a subset of the f.Threshold that's what >= 0 means from string.Compare 0 == match, 1 == string a is greater than string b (not by numerical value) Commented May 17, 2011 at 14:14

3 Answers 3

1

Why not ditch the string compare and explicitly check?

index = llCodeList.FindIndex(
            f => f.Threshold.ToString()
                            .Contains(searchText));

Are you sure that the search text is a subset of the threshold number?

Also it may be that you're not understanding how string.Compare works, it won't check numerical value but string value. For the above if searchText is 4 and the threshold is -40 would match your predicate. My example more explicitly demonstrates the behavior of string.Compare(...,...,...) >= 0

If you're trying to find results based on having a greater than or equal to match on threshold and searchText you could do this

int search = 0;
int32.TryParse(searchText, out search);
index = llCodeList.FindIndex(f => f.Threshold >= search);
Sign up to request clarification or add additional context in comments.

Comments

0

If my comment correctly got the gist of what you are trying to do then do something like this:

int index = -1;
int intVal;
if (int.TryParse(seachText, intVal)){
  index = llCodeList.FindIndex(f => f.Threshold >= intVal);
}

Comments

0

I think you meant to check IndexOf instead of using Compare.

index = llCodeList.FindIndex(
    f => f.Threshold.ToString().IndexOf(
        searchText, StringComparison.InvariantCultureIgnoreCase) >= 0);

But the name FindIndex sounds like you'd want to return the actual index. My crystal ball says you're looking for something like

index = llCodeList.FindIndex(
    f => f.Threshold.ToString().IndexOf(
        searchText, StringComparison.InvariantCultureIgnoreCase));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.