惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
F
Fortinet All Blogs
量子位
G
Google Developers Blog
J
Java Code Geeks
N
Netflix TechBlog - Medium
博客园 - 聂微东
宝玉的分享
宝玉的分享
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
月光博客
月光博客
The Cloudflare Blog
Apple Machine Learning Research
Apple Machine Learning Research
爱范儿
爱范儿
雷峰网
雷峰网
M
MIT News - Artificial intelligence
T
Tailwind CSS Blog
V
Visual Studio Blog
阮一峰的网络日志
阮一峰的网络日志
博客园 - 三生石上(FineUI控件)
Microsoft Azure Blog
Microsoft Azure Blog
aimingoo的专栏
aimingoo的专栏
Martin Fowler
Martin Fowler
有赞技术团队
有赞技术团队
T
The Blog of Author Tim Ferriss

博客园 - LutzMark

charles4抓https请求的注意事项,补充iphone7(ios10系统)无法解密ssl问题 XCode设置自己windows习惯的快捷键(比如Home、End键) MVC项目中ExecutionTimeout不生效的解决方案 Delegate与Event的区别 坑爹的MSN登录错误80072745 LINQ to SQL自定义映射表关系(1:N or 1:1) LINQ to SQL 外键约束的插入及获取主表标识列等问题 Flex中的 for in 与 for each in - LutzMark Flex的DataGrid将水平分隔线设为虚线 - LutzMark - 博客园 解决Flex的DataGrid控件中ItemRender随Scrollbar的滚动发生UI重绘问题 - LutzMark - 博客园 去除ColumnChart自带的阴影效果 - LutzMark - 博客园 IIS7中配置WebOrb支持RTMPT 使用DataAdpater自动批量更新DataSet中的数据到数据库 委托的Invoke 和 BeginInvoke 与Control的Invoke和BeginInvoke(转-因为写得很好) 控制台测试异步委托 一篇好文,以在迷茫时阅读 驳《IT开发工程师的悲哀》 钢板与表针 不用临时变量,只用11个字符交换两个变量的值——窥视C#编译原理的冰山一角
Linq的sum函数InvalidOperationException异常解决办法
LutzMark · 2010-04-21 · via 博客园 - LutzMark

表A中有一列StorageByte(int, not null),求和

A.Where(a => a.JobId ==@jodid).Select(c=>c.StorageByte).Sum()

如果这个@jodid不存在,程序会报InvalidOperationException:The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type.

解决方案:

1.

public int GetSum(int jobId)

{

if (! A.Any(a => a.JobId == jobId)) return 0;

return A.Where(a => a.JobId ==jobId).Select(c=>c.StorageByte).Sum();

}

虽然能解决问题,但毕竟需要一次查询,于是想出方案2

2.

public int GetSum(int jobId)

{

return A.Where(a => a.JobId ==jobId).Select(c=>c.StorageByte).ToArray().Sum()

//or

//return A.Where(a => a.JobId ==jobId).Select(c=>c.StorageByte).ToList().Sum()

}