-2

I have a list "otherfee" on which I am using a LINQ statement but it is showing error while I am matching o.ShortName

 IList <P_OtherFee> otherfee = 
     P_OtherFee.FindByP_ProgramId((int)p_LoanApplication.P_ProgramId);

        int fee = otherfee.Where(o => o.ShortName == 'PROCESSING FEE').Select(o => o);
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
BlackCat
  • 1,932
  • 3
  • 19
  • 47

3 Answers3

3

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.

StackLloyd
  • 409
  • 2
  • 9
2

Because you are selecting the all object (o => o) and not an int property, you cannot set the result of your Where clause in your fee variable.

If you want to select one int :

int fee = otherfee.Select(o => o.intProperty).FirstOrDefault(o => o.ShortName == "PROCESSING FEE");

If you want to select several ints :

IEnumerable<int> fee = otherfee.Where(o => o.ShortName == "PROCESSING FEE").Select(o => o.intProperty);

And as @Sebastian Hofmann mentionned, you cannot use '' to declare a string, you have to use "".

Skrface
  • 1,388
  • 1
  • 12
  • 20
  • 3
    I think this code would not work, because you can't implicitely convert `IEnumerable` to `int`... And there's still the `'PROCESSING FEE'` typo. – Sebastian Hofmann May 16 '19 at 07:28
  • 1
    That's right, I answered too fast. I will edit my answer. – Skrface May 16 '19 at 07:30
  • The first line of code doesn't compile, while the second one does, but it needs yet a few more lines to check the results and assign one to an int variable. – StackLloyd May 16 '19 at 07:58
  • @PrasadTelkikar It would indeed be more efficient to Select before getting the results. I will update my answer. – Skrface May 16 '19 at 08:24
2

You can use Single()/SingleOrDefault() or First()/FirstOrDefault() to get fee value

var feeObj = otherfee.Single(o => o.ShortName == "PROCESSING FEE");  //If otherfee does not contain 'PROCESSING FEE' then it will throw an error
int fee = feeObj.ProcessingFee; 

Or

int? fee = otherfee.FirstOrDefault(o => o.ShortName == "PROCESSING FEE")?.FeeProcessing;

Or if you dont want use nullable data type of int then

int fee = otherfee.FirstOrDefault(o => o.ShortName == "PROCESSING FEE")?.FeeProcessing ?? 0;

You can read difference between Single() and First(): Here

In last code, I used ?. and ??, more more details read about navigation operator and null-coalescing operator in C#.

Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
  • The first two lines of code can be dangerous because you aren't handling any of the possible outcomes (SingleOrDefault is a safer option), while the other line of code doesn't compile (but I guess the "?" is a typo). – StackLloyd May 16 '19 at 08:08
  • @StackLloyd, I already mentioned in comment about possible error while using `single'. `No ?. is not a typo just read about **navigation operator in C#** – Prasad Telkikar May 16 '19 at 08:09
  • I know about the operator, but this means you also need to declare `fee` as `int?`, which you didn't, and such a cast is unnecessary for such a simple operation. – StackLloyd May 16 '19 at 08:23
  • You can simplify it with `??`operator: `int fee = otherfee.FirstOrDefault(o => o.ShortName == "PROCESSING FEE")?.FeeProcessing ?? 0;` – asd May 16 '19 at 09:29