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().开发者_JS百科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.
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.
精彩评论