博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
解决Linq Join Group by 时报错:Nullable object must have a value.
阅读量:6890 次
发布时间:2019-06-27

本文共 4348 字,大约阅读时间需要 14 分钟。

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

解决方法:

///     ///  解决问题: efcore group new dynamic对象时生成int 型而不是 int? 而导致 Nullable object must have a value.的问题    ///     public class GroupTableViewModel    {        ///         ///  Key        ///         public int? Id { get; set; }        ///         /// Count        ///         public int? Count { get; set; }        ///         /// Sum        ///         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()

问题解决

转载于:https://www.cnblogs.com/WNpursue/p/10870540.html

你可能感兴趣的文章
Elasticsearch refresh vs. flush
查看>>
质量管理:测试基础架构图
查看>>
Windows Server 2008安装SQL Server 2008
查看>>
jsonp的原理?
查看>>
快速开发框架V0.001(免费、100%开源)
查看>>
文件上传与下载/Mail
查看>>
jQuery ajax() 方法
查看>>
怎样安排您的读书时间?
查看>>
IoC
查看>>
一、 Python的基本概念
查看>>
子元素margin影响父元素的问题
查看>>
MUI功能列表
查看>>
为什么沃尔玛和其他大型零售商正联手创建移动支付?
查看>>
ios注册通知NSNotificationCenter(一)
查看>>
poj 3252 Round Numbers (组合数)
查看>>
求两个长度相等的排序数组的上中位数
查看>>
video 全屏时 隐藏controls
查看>>
利用腾讯云为你的域名申请并配置免费SSL一年
查看>>
【转】asp.net 利用Global.asax 捕获整个解决方案中的异常错误
查看>>
一道算法题-换钱
查看>>