Friday, August 12, 2022

[FIXED] how to check for a value in a list of decimal C#

Issue

how to check if a decimal value exists in a list of decimal C#.

I want to achieve the following, but I am looking for right way to compare a decimal value from a list of decimals.

decimal value = 100;
List<decimal > Amounts = new List<decimal>() { 20, 30 };
I want to compare if 
Amounts.Any(value)
//do something
else
do something

Solution

You can use the .Find() method from here:

List.Find(Predicate) Method

Example:

decimal valueToFind = 100;
List<decimal> amounts = new List<decimal>() { 20, 30 };
var result = amounts.Find(x => x == valueToFind);

if (result == 0){
    //not found
}
else if (result == valueToFind){
    //found
}


Answered By - troyw1636
Answer Checked By - Timothy Miller (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.