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.的更多相关文章

  1. 解决ThinkPHP关闭调试模式时报错的问题汇总

    解决ThinkPHP关闭调试模式时报错的问题汇总 案例一: 最近用ThinkPHP开发一个项目,本地开发测试完成上传到服务器后,第一次打开正常,再刷新页面时就出现 "页面调试错误,无法找开页 ...

  2. 修改 docker image 安装目录 (解决加载大image时报错:"no space left on device")

    修改 docker image 安装目录 (解决加载大image时报错:"no space left on device" ) 基于Ubuntu16.04 docker版本: 17 ...

  3. 解决使用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 ...

  4. 安装Django时报错'module' object has no attribute 'lru_cache'

    使用pip方法安装Django时报错'module' object has no attribute 'lru_cache' 解决办法如下 命令行输入命令sudo pip install Django ...

  5. 如何解决git创建密匙时报错Too many arguments

    如题:git创建密匙时报错Too many arguments. 前几天我遇见了一个问题,git需要重新创建密匙,运行命令ssh-keygen -t rsa -b 4096 -C " you ...

  6. 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 ...

  7. 【原创】基于部署映像服务和管理(DISM)修改映象解决WIN7 USB3.0安装时报错

    本文作者为博客园阿梓喵http://www.cnblogs.com/c4isr/,转载请注明作者. 本文源地址:http://www.cnblogs.com/c4isr/p/3532362.html ...

  8. 解决Eclipse Pydev中import时报错:Unresolved import

    在安装 图像处理工具包 mahotas 后,在eclipse中尝试import mahotas时,出现Unresolved import错误,按快捷无法自动生成代码提示 但是,程序运行时可以通过,在命 ...

  9. 解决 安装或卸载软件时报错Error 1001 的问题

    卸载或安装程序时出错1001:错误1001可能发生在试图更新.修复或卸载windows os中的特定程序时.此问题通常是由于程序的先前安装损坏而引起的. 错误“1001”通常会遇到,因为程序的先前安装 ...

随机推荐

  1. Fork/Join框架与Java8 Stream API 之并行流的速度比较

    Fork/Join 框架有特定的ExecutorService和线程池构成.ExecutorService可以运行任务,并且这个任务会被分解成较小的任务,它们从线程池中被fork(被不同的线程执行)出 ...

  2. 《linux就该这么学》课堂笔记18 squid服务

    Squid服务程序正向解析和反向解析 正向代理模式不仅可以让用户使用Squid代理服务器上网,还可以基于指定的IP地址.域名关键词.网站地址或下载文件后缀等信息,实现类似于访问控制列表的功能.反向代理 ...

  3. 不同平台下int类型、指针类型的数据大小

    不同平台下int类型.指针类型的数据大小 对于int类型数据和指针类型数据的大小,是非常基础的问题. 在一个具体的平台上,确定他们最好的办法就是使用sizeof(type)对其进行判断,返回当前数据类 ...

  4. 配置管理-git研究(版本管理)

    1. 安装git2.7 git2.7具体安装步骤如下: [root@host1 ~]# yum install curl-devel expat-devel gettext-devel openssl ...

  5. 十、lambda表达式、内置函数之filter、map、reduce

    lambda表达式   学习条件运算时,对于简单的 if else 语句,可以使用三元运算来表示,即: # 普通条件语句 == : name = 'wupeiqi' else: name = 'ale ...

  6. 设置linux代理完成apt-get

    最近ubuntu的服务器被公司关闭了外网使用权限,但是安装软件又需要连接外网,那么就只能通过代理来连接了. 先按照下面的这篇帖子来设置windows端的代理. https://blog.csdn.ne ...

  7. 《Pro Continuous Delivery With Jenkins 2.0》随书笔记

    今天同时看完<Pro Continuous Delivery With Jenkins 2.0>, 这书与工作关系很大,但也是快速翻翻. 本书着重点jenkins高可用环境搭建,与gith ...

  8. Browse Princeton's Series (by Date) in Princeton Economic History of the Western World

    Browse Princeton's Series (by Date) in Princeton Economic History of the Western World Joel Mokyr, S ...

  9. cocos2dx+KinectV2 体感游戏之微信打飞机

    https://download.csdn.net/download/qq_34609108/10038417 https://blog.csdn.net/qq_34609108/article/de ...

  10. 【JZOJ5738】【20190706】锁屏杀

    题目 $n \le 2000 $ 题解 \(B\)的数字可以对1440取模,对三个图分别进行\(dp\)即可 时间复杂度\(O(n\times 1440 \times 10)\) Code #incl ...