[{"data":1,"prerenderedAt":130},["ShallowReactive",2],{"/2025/12/13-ef-sum-error-due-to-empty-rows-after-filtering-on-mysql":3},{"id":4,"title":5,"body":6,"date":121,"description":49,"extension":122,"meta":123,"navigation":124,"path":125,"robots":126,"seo":127,"stem":128,"__hash__":129},"posts/2025/12/13 EF-sum-error-due-to-empty-rows-after-filtering-on-MySQL.md","13 EF Sum Error Due To Empty Rows After Filtering On MySQL",{"type":7,"value":8,"toc":118},"minimark",[9,18,25,28,31,33,36,50,52,55,63,65,68,76,78,81,94,110],[10,11,13],"post-title",{":date":12},"date",[14,15,17],"h1",{"id":16},"ef-sum-error-due-to-empty-rows-after-filtering-on-mysql","EF Sum Error Due to Empty Rows After Filtering on MySQL",[19,20,21],"notes",{},[22,23,24],"p",{},"This is a repost from my old blog. First posted in 7/23/2020.",[26,27],"br",{},[22,29,30],{},"I actually have been waiting when I will encounter this kind of error. This time the error happens when performing EF query on MySQL database.",[26,32],{},[22,34,35],{},"After filtering, the query returns empty rows thus sum can't work. The error message in my case is:",[37,38,39],"code-block",{},[40,41,46],"pre",{"className":42,"code":44,"language":45},[43],"language-text","\"The cast to value type 'System.Decimal' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.\"\n","text",[47,48,44],"code",{"__ignoreMap":49},"",[26,51],{},[22,53,54],{},"The following is an example code:",[37,56,57],{},[40,58,61],{"className":59,"code":60,"language":45},[43],"Dim totalPrice = dbContext.Items.Where(Function(i) i.Color = \"Blue\").Sum(Function(i) i.Price)\n",[47,62,60],{"__ignoreMap":49},[26,64],{},[22,66,67],{},"However it works fine if we execute the query first before Sum, but it requires the rows to be pulled to memory which can be resource intensive.",[37,69,70],{},[40,71,74],{"className":72,"code":73,"language":45},[43],"Dim totalPrice = dbContext.Items.Where(Function(i) .Color = \"Blue\").ToList().Sum(Function(i) i.Price)\n",[47,75,73],{"__ignoreMap":49},[26,77],{},[22,79,80],{},"One helpful article:",[22,82,83],{},[84,85,88],"span",{"className":86},[87],"text-blue-600",[89,90,91],"a",{"href":91,"rel":92},"https://coding.abel.nu/2012/08/null-sematics-in-linqs-sum/",[93],"nofollow",[22,95,96,97,104,105,109],{},"The solution in my case is to perform projection, followed by ",[47,98,103],{"className":99},[100,101,102],"bg-gray-200","p-2","rounded","DefaultIfEmpty"," and call ",[47,106,108],{"className":107},[100,101,102],"Sum()"," afterwards. The code becomes:",[37,111,112],{},[40,113,116],{"className":114,"code":115,"language":45},[43],"Dim totalPrice = dbContext.Items.Where(Function(i) i.Color = \"Blue\").Select(Function(i) i.Price).DefaultIfEmpty(Decimal.Zero).Sum()\n",[47,117,115],{"__ignoreMap":49},{"title":49,"searchDepth":119,"depth":119,"links":120},2,[],"2025-12-13T00:00:00.000Z","md",{},true,"/2025/12/13-ef-sum-error-due-to-empty-rows-after-filtering-on-mysql",null,{"title":5,"description":49},"2025/12/13 EF-sum-error-due-to-empty-rows-after-filtering-on-MySQL","3VrQ-6mFaedI0gVb9BzWZZ7jiF134aBjfOGqdWTvgoc",1785167452188]