开发者

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

开发者 https://www.devze.com 2023-04-09 10:40 出处:网络
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<d

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消