Kinyanjui Kamau
Kinyanjui Kamau

Reputation: 1986

operator '-' cannot be applied to operands of type 'decimal' and 'Systems.Collections.Generic.IEnumerable<decimal>

I have this error that I am tying to solve.

Visual Studio 2010 gives the error as:

operator '-' cannot be applied to operands of type 'decimal' and 'Systems.Collections.Generic.IEnumerable<decimal>

My Code:

// Step 5: Retrieve MPR amount and deduct form Gross Tax Dictionary Per-Employee

                //Query database
                var taxtable = taxTableService.GetAllTaxTables();

                var mprAmount = (from tt in taxtable select tt.U_MPR_amount).Distinct();

                Dictionary<int, decimal> paye = new Dictionary<int, decimal>();

                grossTaxDictionary.ToList().ForEach(x =>
                {
                    var totalPAYE = x.Value - mprAmount;

                    paye.Add(x.Key, totalPAYE);

                });

In my database, the field U_MPR_amount is a decimal and so is x.Value.

Error shows up on the line x.Value - mprAmount;

What could be the problem? Any help appreciated.

Upvotes: 3

Views: 2871

Answers (1)

Konrad Rudolph
Konrad Rudolph

Reputation: 546153

According to your code, mprAmount is a list, you cannot subtract it from a single value.

If you are certain that the list contains just a single element then you can retrieve it using the Single method:

var totalPAYE = x.Value - mprAmount.Single();

Or, more likely:

var mprAmount = (from tt in taxtable select tt.U_MPR_amount).Single();

Notice that I’ve changed Distinct for Single. Using both makes no real sense.

Upvotes: 7

Related Questions