使用强制转换时指定的强制转换无效

发布于 2024-11-27 20:53:08 字数 1180 浏览 1 评论 0 原文

当尝试获取此 Linq 查询中的总和时,我收到以下错误:

InvalidCastException 未处理。指定的演员阵容无效。

我使用 DataType 属性来三次检查该列是否实际上是 Double,而且确实如此。

foreach (DataColumn item in _dttMasterViewTransaction.Columns)
{
    if (item.ColumnName == "Dr")
    {
        //Outputs: System.Double!
        MessageBox.Show(item.DataType.ToString());
    }
}

var datos = _dttMasterViewTransaction.AsEnumerable().Where(r => (int)r["Entity"] == FundsID).Select(r => new EntityJESummary()
{
    JEId = (int)r["JE ID"],
    JEGroupingId = (int)r["JE Group"],
    PartnershipId = (int)r["Entity"],
    BookingDate = Convert.ToDateTime(r["GL Date"]),
    EffectiveDate = Convert.ToDateTime(r["Effective Date"]),
    Allocated = Convert.ToBoolean(r["Allocated"]),
    JEEstate = (int)r["JE State"],
    JEComments = r["JE Comments"].ToString(),

    Debit = _dttMasterViewTransaction.AsEnumerable().Where(s => (int)r["Entity"] == FundsID).Sum(s => (double)s["Dr"]),
    Credit = _dttMasterViewTransaction.AsEnumerable().Where(s => (int)r["Entity"] == FundsID).Sum(s => (double)s["CR"])

}).First();

关于为什么会发生这种情况有什么建议吗?

I'm getting the following error when trying to get the sum in this Linq query:

InvalidCastException was unhandled. Specified cast is not valid.

I used the DataType property to triple check is that column is in fact a Double, and it is.

foreach (DataColumn item in _dttMasterViewTransaction.Columns)
{
    if (item.ColumnName == "Dr")
    {
        //Outputs: System.Double!
        MessageBox.Show(item.DataType.ToString());
    }
}

var datos = _dttMasterViewTransaction.AsEnumerable().Where(r => (int)r["Entity"] == FundsID).Select(r => new EntityJESummary()
{
    JEId = (int)r["JE ID"],
    JEGroupingId = (int)r["JE Group"],
    PartnershipId = (int)r["Entity"],
    BookingDate = Convert.ToDateTime(r["GL Date"]),
    EffectiveDate = Convert.ToDateTime(r["Effective Date"]),
    Allocated = Convert.ToBoolean(r["Allocated"]),
    JEEstate = (int)r["JE State"],
    JEComments = r["JE Comments"].ToString(),

    Debit = _dttMasterViewTransaction.AsEnumerable().Where(s => (int)r["Entity"] == FundsID).Sum(s => (double)s["Dr"]),
    Credit = _dttMasterViewTransaction.AsEnumerable().Where(s => (int)r["Entity"] == FundsID).Sum(s => (double)s["CR"])

}).First();

Any suggestions on why this could occur?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

断爱 2024-12-04 20:53:08

该字段的任何条目是否为空?还是有一些不是双打?

我们需要查看更多数据才能得到更好的答案......

Are any of the entries null for that field? or are there some that are not doubles?

we'd need to see more data to get a better answer...

や莫失莫忘 2024-12-04 20:53:08

如果值可以为 null 或空字符串,请尝试以下操作:

Debit = _dttMasterViewTransaction.AsEnumerable().Where(s => (int)r["Entity"] == FundsID).Sum(s => (String.IsNullOrEmpty(s["Dr"]) ? 0d : (double)s["Dr"])),
Credit = _dttMasterViewTransaction.AsEnumerable().Where(s => (int)r["Entity"] == FundsID).Sum(s => (String.IsNullOrEmpty(s["CR"]) ? 0d : (double)s["CR"]))

If the values can be either null or an empty string, try this:

Debit = _dttMasterViewTransaction.AsEnumerable().Where(s => (int)r["Entity"] == FundsID).Sum(s => (String.IsNullOrEmpty(s["Dr"]) ? 0d : (double)s["Dr"])),
Credit = _dttMasterViewTransaction.AsEnumerable().Where(s => (int)r["Entity"] == FundsID).Sum(s => (String.IsNullOrEmpty(s["CR"]) ? 0d : (double)s["CR"]))
剧终人散尽 2024-12-04 20:53:08

您可能想要使用可空形式的 Sum。

Debit = _dttMasterViewTransaction.AsEnumerable().Where(s => (int)r["Entity"] == 
FundsID).Sum(s => (double?)s["Dr"]),     
Credit = _dttMasterViewTransaction.AsEnumerable().Where(s => (int)r["Entity"] == 
FundsID).Sum(s => (double?)s["CR"])

检查此链接以获取更多详细信息:

http://weblogs.asp.net/zeeshanhirani/archive/2008/07/15/applying-aggregates-to-empty-collections-causes-exception-in-linq-to-sql.aspx< /a>

You may want to use Nullable form of Sum.

Debit = _dttMasterViewTransaction.AsEnumerable().Where(s => (int)r["Entity"] == 
FundsID).Sum(s => (double?)s["Dr"]),     
Credit = _dttMasterViewTransaction.AsEnumerable().Where(s => (int)r["Entity"] == 
FundsID).Sum(s => (double?)s["CR"])

Check this link for more details:

http://weblogs.asp.net/zeeshanhirani/archive/2008/07/15/applying-aggregates-to-empty-collections-causes-exception-in-linq-to-sql.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文