Skip to content

The null value cannot be assigned to a member with type System.Int64 which is a non-nullable value type

Are you getting this?

System.InvalidOperationException:
The null value cannot be assigned to a member with type System.Int64 which is a non-nullable value type.

On something like this?

var totalSize = query.Sum(x => x.Size);

This occurs because your LINQ property (in my case “Size”) is not nullable, and needs to be nullable because your Sum() operation could potentially iterate over a list which is empty yielding your LINQ property value to be null.
Of course you can’t Sum() null as we know or the universe would explode, so try this instead:

var totalSize = query.Sum(x => (long?) x.Size);

This may very well just be a LINQ to SQL thing, but I am not sure. Maybe somebody else out there in the wide world web can enlighten me!

Best of luck!

VN:F [1.9.10_1130]
Rating: 4.3/5 (8 votes cast)
VN:F [1.9.10_1130]
Rating: -1 (from 3 votes)
The null value cannot be assigned to a member with type System.Int64 which is a non-nullable value type, 4.3 out of 5 based on 8 ratings
Bookmark and Share
kick it on DotNetKicks.com
Shout it

NOW, FOR A WORD FROM OUR SPONSORS

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*

My name is Graham O'Neale and I'm a software architect from Gold Coast, Australia. I am an overtime thinker, full time coder and awake part time in the real world. I have a keen interest in software development, particularly in the realm of programming (C#, ASP.NET, ASP.NET MVC, LINQ (2 SQL), Entity Framework, Silverlight, Blend, WCF, WPF) and a keen interest in the cutting edge and innovation. I have a new found love for design patterns, ALT.NET practices and well crafted software architecture. The purpose of this blog is to express any thoughts, findings, tips and gripes along my travels in the wonderful world of coding and technology...