解决Linq Join Group by 时报错:Nullable object must have a value.
Linq Join Group by 时报Nullable object must have a value.
例如:
from s in subject on ch.SubId equals s.SubId
join gc in (from aq in question
group aq by aq.ChapterId
into gaq
select new
{
Id = gaq.Key,
Count = gaq.Count(),
})
on s.QueId equals gc.Id
into gc2
from gc in gc2.DefaultIfEmpty()
结果将会报错
生成的sql语句符合预期,为简单的join
解决方法:
/// <summary>
/// 解决问题: efcore group new dynamic对象时生成int 型而不是 int? 而导致 Nullable object must have a value.的问题
/// </summary>
public class GroupTableViewModel
{
/// <summary>
/// Key
/// </summary>
public int? Id { get; set; }
/// <summary>
/// Count
/// </summary>
public int? Count { get; set; }
/// <summary>
/// Sum
/// </summary>
public int? Sum { get; set; }
public int? Max { get; set; }
}
加上 GroupTableViewModel 问题解决
from s in subject on ch.SubId equals s.SubId
join gc in (from aq in question
group aq by aq.ChapterId
into gaq
select new GroupTableViewModel
{
Id = gaq.Key,
Count = gaq.Count(),
})
on s.QueId equals gc.Id
into gc2
from gc in gc2.DefaultIfEmpty()
解决Join 多值报错
from employee in employees
join student in students
on new { employee.FirstName, employee.LastName } equals new { student.FirstName, student.LastName }
select employee.FirstName + " " + employee.LastName;
例如
from u in User
join sac in (from sa in Answer
join e in Exam on sa.ExamId equals e.ExamId
group sa by new { UserId = sa.UserId, ExamId = sa.ExamId, }
into gsa
select new
{
UserId = gsa.Key.UserId,
ExamId = gsa.Key.ExamId,
ScoreCount = gsa.Sum(x => x.Score),
})
on new { UserId = u.UserId, ExamId = ue.ExamId } equals new { UserId = sac.UserId, ExamId = sac.ExamId }
into sac2
from sac in sac2.DefaultIfEmpty()
生成sql语句正常 但报错 Nullable object must have a value.
解决方法:
public class GroupExam
{
public int? UserId { get; set; }
public int? ExamId { get; set; }
public decimal? ShortAnswerScoreCount { get; set; }
}
将原linq修改为
from u in User
join sac in (from sa in Answer
join e in Exam on sa.ExamId equals e.ExamId
group sa by new { UserId = sa.UserId, ExamId = sa.ExamId, }
into gsa
select new GroupExam
{
UserId = gsa.Key.UserId,
ExamId = gsa.Key.ExamId,
ScoreCount = gsa.Sum(x => x.Score),
})
on new { UserId = u.UserId, ExamId = ue.ExamId } equals new { UserId = (int)sac.UserId, ExamId = sac.ExamId }
into sac2
from sac in sac2.DefaultIfEmpty()
问题解决
解决Linq Join Group by 时报错:Nullable object must have a value.的更多相关文章
- 解决ThinkPHP关闭调试模式时报错的问题汇总
解决ThinkPHP关闭调试模式时报错的问题汇总 案例一: 最近用ThinkPHP开发一个项目,本地开发测试完成上传到服务器后,第一次打开正常,再刷新页面时就出现 "页面调试错误,无法找开页 ...
- 修改 docker image 安装目录 (解决加载大image时报错:"no space left on device")
修改 docker image 安装目录 (解决加载大image时报错:"no space left on device" ) 基于Ubuntu16.04 docker版本: 17 ...
- 解决使用DBeaver连接MySQL时报错-The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone.
解决使用DBeaver连接MySQL时报错,其实提示很明显. The server time zone value '�й���ʱ��' is unrecognized or represents ...
- 安装Django时报错'module' object has no attribute 'lru_cache'
使用pip方法安装Django时报错'module' object has no attribute 'lru_cache' 解决办法如下 命令行输入命令sudo pip install Django ...
- 如何解决git创建密匙时报错Too many arguments
如题:git创建密匙时报错Too many arguments. 前几天我遇见了一个问题,git需要重新创建密匙,运行命令ssh-keygen -t rsa -b 4096 -C " you ...
- linux上,mysql使用聚合函数group by 时报错:SELECT list is not in GROUP BY clause and contains nonaggre的问题
之前在windows上测试是可以正常使用的,但是上传到Linux上后,就报错: Expression # of SELECT list is not in GROUP BY clause and co ...
- 【原创】基于部署映像服务和管理(DISM)修改映象解决WIN7 USB3.0安装时报错
本文作者为博客园阿梓喵http://www.cnblogs.com/c4isr/,转载请注明作者. 本文源地址:http://www.cnblogs.com/c4isr/p/3532362.html ...
- 解决Eclipse Pydev中import时报错:Unresolved import
在安装 图像处理工具包 mahotas 后,在eclipse中尝试import mahotas时,出现Unresolved import错误,按快捷无法自动生成代码提示 但是,程序运行时可以通过,在命 ...
- 解决 安装或卸载软件时报错Error 1001 的问题
卸载或安装程序时出错1001:错误1001可能发生在试图更新.修复或卸载windows os中的特定程序时.此问题通常是由于程序的先前安装损坏而引起的. 错误“1001”通常会遇到,因为程序的先前安装 ...
随机推荐
- Linux的httpd服务搭建
在服务搭建前,还要了解一下httpd的日志. 日志有助有工作人员,查看服务器出错状况,更能统计数据分析网页运行情况. PV和UV两大分析 PV Page View 页面访问量 UV User Vi ...
- Docker在Centos 7上的部署
Docker在Centos 7上的部署 方法1---开启centos 7上自带的 extras YUM源,然后 yum install docker来安装 安装前必须保证 Linux Kernel ...
- 《linux就该这么学》课堂笔记16 bind域名解析
1.DNS域名解析服务 DNS(Domain Name System,域名系统)是一项用于管理和解析域名与IP地址对应关系的技术,就是能够接受用户输入的域名或IP地址,然后自动查找与之匹配(或者说具有 ...
- mysql 命令修复表
如果在断电.硬重启服务器等情况下,可能会造成有些表损坏,这时就需要修复 修复表前需要将mysql停止 centos7 用 systemctl stop mysqld.service 来停止 然后进入到 ...
- QElapsedTimer定时器记录程序执行时间
QElapedTimer类提供了一种快速计算运行时间的方法,从Qt4.7引入. QElapsedTimer类通常用于快速计算两个事件之间经过了多少时间.它的API与QTime相似,因此可以将正在使用的 ...
- 使用WIFI网卡iw
上篇博客中,配置修改了内核,以支持所选择的USB网卡,本篇博客需要去编写一些应用程序,将wifi网卡使用起来. 1.1 概念:认证/加密认证:就是用来判断哪些用户可以使用这个无线网络加密:是指手机和A ...
- 对象存储服务 OSS(Object Storage Service),知识点(待补充上仓库代码)
资料 网址 官方文档 https://help.aliyun.com/product/31815.html?spm=a2c4g.11186623.3.1.3e1459669xRokl OSS Brow ...
- Hbase与hive集成与对比
HBase与Hive的对比 1.Hive (1) 数据仓库 Hive的本质其实就相当于将HDFS中已经存储的文件在Mysql中做了一个双射关系,以方便使用HQL去管理查询. (2) 用于数据分析.清洗 ...
- 三星固态Dell版的960g的sm863a硬盘
smart参数 CrystalDiskMark测试 AS SSD 测试 HD Tune Pro测试 DiskGenius查看 总结: 按我的测试,性能比sm865的还好,不知道咋回事,按三星给的参数这 ...
- 2017-2018 ACM-ICPC Southeastern European Regional Programming Contest (SEERC 2017)
2017-2018 ACM-ICPC Southeastern European Regional Programming Contest (SEERC 2017) 全靠 wxh的博客 补完这套.wx ...