c# – operator’ – ‘不能应用于’decimal’和’Systems.Collections.Generic.IEnumerable类型的操作数

我有这个错误,我想要解决.

Visual Studio 2010将错误指定为:

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

我的代码

// 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);

                });

在我的数据库中,字段U_MPR_amount是小数,因此是x.Value.

错误显示在x.Value行 – mprAmount;

可能是什么问题呢?任何帮助赞赏.

解决方法

根据您的代码,mprAmount是一个列表,您不能从单个值中减去它.

如果您确定该列表只包含一个元素,那么您可以使用Single方法检索它:

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

或者,更有可能:

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

请注意,我已经更改了distinct for Single.使用两者都没有任何意义.

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...