微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.使用两者都没有任何意义.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐