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. oarcle wm_concat 值过长解决--使用 clob

    sql 语句替换 :select XMLAGG(XMLELEMENT(a, WSODETAILPALINCD || ',')).EXTRACT('//text()').getclobval() as ...

  2. python基本数据类型的时间复杂度

    1.list 内部实现是数组 2.dict 内部实现是hash函数+哈希桶.一个好的hash函数使到哈希桶中的值只有一个,若多个key hash到了同一个哈希桶中,称之为哈希冲突. 3.set 内部实 ...

  3. nginx 缓存服务

    1.nginx 缓存 upstream imooc { server 116.62.103.228:8001; server 116.62.103.228:8002; server 116.62.10 ...

  4. python基础语法17 面向对象4 多态,抽象类,鸭子类型,绑定方法classmethod与staticmethod,isinstance与issubclass,反射

    多态 1.什么是多态? 多态指的是同一种类型的事物,不同的形态. 2.多态的目的: “多态” 也称之为 “多态性”,目的是为了 在不知道对象具体类型的情况下,统一对象调用方法的规范(比如:名字). 多 ...

  5. 微信H5页面分享获取JS-SDK

    https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115  微信开发文档: 生成签名之前必须先了解一下jsapi_ti ...

  6. DOM操作 三大家族

    clientHeight     获取对象的高度,不计算任何边距.边框.滚动条,但包括该对象的补白.   clientLeft     获取    offsetLeft     属性和客户区域的实际左 ...

  7. 安装Visual Studio IntelliCode提供代码智能提示AI

    The Visual Studio IntelliCode extension provides AI-assisted development features for Python, TypeSc ...

  8. haproxy 配置文件详解 之 backend

    配置示例: backend htmpool mode http option redispatch option abortonclose balance static-rr cookie SESSI ...

  9. 第08组 Beta版本演示

    简介 组名:955 组长博客:点这里! 成员 031702329庄锡荣(组长) 031702309林晓锋 031702309侯雅倩 031702311陈珊珊 171709030吴珂雨 03170231 ...

  10. Apache Beam实战指南 | 大数据管道(pipeline)设计及实践

    Apache Beam实战指南 | 大数据管道(pipeline)设计及实践  mp.weixin.qq.com 策划 & 审校 | Natalie作者 | 张海涛编辑 | LindaAI 前 ...