解决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”通常会遇到,因为程序的先前安装 ...
随机推荐
- Nginx 核心配置-location的匹配案例实战篇
Nginx 核心配置-location的匹配案例实战篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.location语法规则介绍 在没有使用正则表达式的时候,nginx会先在 ...
- Codeforces 749E: Inversions After Shuffle
题目传送门:CF749E. 记一道傻逼计数题. 题意简述: 给一个 \(1\) 到 \(n\) 的排列,随机选取区间 \([l,r]\) 随机打乱区间内的元素,问打乱后的整个序列的逆序数期望. 题解: ...
- Eslint 允许使用双等号
资料 网址 ESlint: Expected !== and instead saw != https://stackoverflow.com/questions/48375316/eslint-ex ...
- js提取DOM属性和设置DOM属性值
<style type="text/css"> #div1{width:100px;height:100px;} #div2{background} </styl ...
- docker删除镜像Error response from daemon: conflict: unable to remove repository reference
Docker无法删除images,由于是依赖container. 1.进入root权限 sudo su 2. 列出所有运行或没有运行的镜像 docker ps -a 3.停止containe ...
- Mybatis「MySQL-Oracle」 中主键自动生成 <selectKey> 序列化
有时候我们不仅仅是通过返回 int 影响行数来确定数据是否插入成功就行了,因为我们总是会用到这个刚刚插入的自增主键,比如主子表入库,子表需要主表的 id,那这个时候我们再去数据库查就显得有点 low ...
- 28 让树莓派开机“说”出自己的IP地址
http://shumeipai.nxez.com/2019/02/02/analogue-audio-redux.html 树莓派音频口底噪消除的方法
- MongoDB数据操作练习
1.创建一年级的3个班,并随机添加 10 名学生: >for(grade_index in (grade = ['grade_1_1', 'grade_1_2', 'grade_1_3'])) ...
- [RN] React Native 自定义导航栏随滚动渐变
React Native 自定义导航栏随滚动渐变 实现效果预览: 代码实现: 1.定义导航栏 NavPage.js import React, {Component} from 'react'; im ...
- 09-排序2 Insert or Merge (25 分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...