When Executing the following linq to sql statement:

执行以下linq to sql语句时:

    var stuff = from l in _db.SqlLinks
                select new
                           {
                               Link = l,
                               Rating = (from v in l.SqlLinkVotes
                                         where v.Tag == tagId
                                               && v.VoteDate >= since
                                         select v.Vote).Sum(),
                               NumberOfVotes = (from v in l.SqlLinkVotes
                                                where v.Tag == tagId
                                                      && v.VoteDate >= since
                                                select v.Vote).Count(),
                               NumberOfComments = (from v in l.SqlLinkVotes
                                                   where v.Tag == tagId
                                                         && v.VoteDate >= since
                                                         && v.Comment != ""
                                                   select v.Vote).Count()
                           };

I get a System.InvalidOperationException (null value cannot be assigned to Int32).

我得到一个System.InvalidOperationException(无法将null值赋给Int32)。

Through debugging I've seen that this comes from the Rating property of the dynamic object.

通过调试我发现它来自动态对象的Rating属性。

When there are no SqlLinkVotes for a particular link the Sum() results in a null value, but Rating is an int, and linq to sql thinks the Sum() will result in an int, not a nullable int.

当没有特定链接的SqlLinkVotes时,Sum()会产生一个空值,但是Rating是一个int,而linq到sql认​​为Sum()会产生一个int,而不是一个可以为null的int。

I could easily write a stored procedure to get around this, but I thought it was a good way for me to understand linq to sql more.

我可以轻松地编写一个存储过程来解决这个问题,但我认为这是一个很好的方式让我更了解linq到sql。

Please help!

2 个解决方案

#1


There's a Connect thread about this which suggests that you cast the result of Sum() to a nullable type (int? in your case). I suspect if you want the rating to be non-nullable, you can then use the null-coalescing operator:

有关于此的Connect线程,这表明您将Sum()的结果转换为可以为null的类型(在您的情况下为int?)。我怀疑如果你想让评级不可为空,你可以使用null-coalescing运算符:

Rating = ((int?) (from v in l.SqlLinkVotes
                  where v.Tag == tagId
                     && v.VoteDate >= 
                  select v.Vote).Sum()) ?? 0

It's worth a try, anyway.

无论如何,值得一试。

更多相关文章

  1. 每条sql语句实际上都是一个事物(事物多种类型解读)
  2. sql 语句 更改默认的sql server 数据库
  3. 怎么用SQL语句实现表中的一个字段加1啊??
  4. PHP获取MySQL执行sql语句的查询时间
  5. 删除2行1 Sql语句mysql
  6. 使用IN语句缓慢mysql删除查询
  7. 1.4.6 收集sql语句的执行计划 2
  8. 利用读写锁实现sqlite多线程写的问题
  9. 在SQL SELECT语句中重用别名字段

随机推荐

  1. zz浅谈android的selector,背景选择器
  2. Android 动态logo bootanimation.zip 制
  3. android 核心组件( 2 )
  4. Android(安卓)addJavaScriptInterface
  5. Android/java 多线程(五)-ThreadPoolExec
  6. Android NDK: WARNING: APP_PLATFORM and
  7. Android MVC模式
  8. Android动态显示具体到秒的相聚时间
  9. android基本概念
  10. Flutter跟Android交互