当前位置:网站首页>Entity framework calls Max on null on records - Entity Framework calling Max on null on records
Entity framework calls Max on null on records - Entity Framework calling Max on null on records
2022-07-04 12:34:00 【fyin1314】
problem :
When calling Max() on an IQueryable and there are zero records I get the following exception. stay IQueryable On the call Max() And recorded as zero , I received the following exception .
The cast to value type 'Int32' failed because the materialized value is null. Convert to value type 'Int32' Failure , Because the materialized value is null.Either the result type's generic parameter or the query must use a nullable type. Generic parameters or queries of result types must use nullable types .
var version = ctx.Entries .Where(e => e.Competition.CompetitionId == storeCompetition.CompetitionId) .Max(e => e.Version);
Now I understand why this happens my question is how is the best way to do this if the table can be empty. Now I understand why this happened , My question is if the table can be empty , The best way is how to do this .The code below works and solves this problem, but its very ugly is there no MaxOrDefault() concept? The following code is effective and solves this problem , But it's very ugly , No, MaxOrDefault() Concept ?
int? version = ctx.Entries .Where(e => e.Competition.CompetitionId == storeCompetition.CompetitionId) .Select(e => (int?)e.Version) .Max();
Solution :
Reference resources : https://stackoom.com/en/question/Djlc边栏推荐
- Decrypt the advantages of low code and unlock efficient application development
- Clion configuration of opencv
- Summary of Shanghai Jiaotong University postgraduate entrance examination module -- cryptography
- Bottom Logic -- Mind Map
- Alibaba cloud server connection intranet operation
- Fastlane 一键打包/发布APP - 使用记录及踩坑
- Global and Chinese market of ice water machines 2022-2028: Research Report on technology, participants, trends, market size and share
- [Android reverse] function interception instance (③ refresh CPU cache | ④ process interception function | ⑤ return specific results)
- Global and Chinese market for naval vessel maintenance 2022-2028: Research Report on technology, participants, trends, market size and share
- Kivy教程之 08 倒计时App实现timer调用(教程含源码)
猜你喜欢
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 22
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 23
TCP slicing and PSH understanding
(August 9, 2021) example exercise of air quality index calculation (I)
French Data Protection Agency: using Google Analytics or violating gdpr
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 13
Summary of Shanghai Jiaotong University postgraduate entrance examination module firewall technology
Flet教程之 02 ElevatedButton高级功能(教程含源码)(教程含源码)
Review of week 278 of leetcode II
nn. Exploration and experiment of batchnorm2d principle
随机推荐
Number and math classes
Reptile learning 4 winter vacation series (3)
French Data Protection Agency: using Google Analytics or violating gdpr
What if the chat record is gone? How to restore wechat chat records on Apple Mobile
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 22
Uva536 binary tree reconstruction tree recovery
Global and Chinese markets for soluble suture 2022-2028: Research Report on technology, participants, trends, market size and share
Lecture 9
R语言--readr包读写数据
2020 Summary - Magic year, magic me
Flet教程之 02 ElevatedButton高级功能(教程含源码)(教程含源码)
Decrypt the advantages of low code and unlock efficient application development
Iframe to only show a certain part of the page
LxC shared directory permission configuration
Anti clockwise rotation method of event arrangement -- PHP implementation
How to disable debug messages on sockjs stomp - how to disable debug messages on sockjs Stomp
Reptile learning winter vacation series (2)
The most robust financial products in 2022
How to use the mongodb ID array to get multiple documents- How to get multiple document using array of MongoDb id?
Summary of Shanghai Jiaotong University postgraduate entrance examination module firewall technology