You are assigning the whole IEnumerable<P_OtherFee>
object to an int
variable.
You should filter the list and retrieve the property from a single object, but beware that it might be null
, if nothing is found based on the conditions.
Here is an example with the ternary operator to assign a default value of 0
in case obj is null
:
P_OtherFee obj = otherFee.FirstOrDefault(o => o.ShortName == "PROCESSING FEE");
int fee = (obj != null) ? obj.Fee : 0;
Old-school LINQ syntax might be simpler to read and understand:
int fee = (from obj in otherFee
where obj.ShortName == "PROCESSING FEE"
select obj.Fee).FirstOrDefault();
If a match is found, you get one Fee
value, if there is no result, you get 0
.
This achieves the same result as the previous code, but saves you from writing a null check.
In both cases, you could use SingleOrDefault(...)
instead of FirstOrDefault(...)
if you expect exactly one result and want an exception to be raised if more than one result is obtained.