Why am I getting an InvalidOperationException with this Linq to Sql method?

In your case). I suspect if you want the rating to be non-nullable, you can then use the null-coalescing operator: Rating = ((int?) (from v in l. SqlLinkVotes where v.

Tag == tagId && v. VoteDate >= select v. Vote).Sum())?

0 It's worth a try, anyway.

Aha! You beat me to it! I just posted the same thing.

– Matthew Rathbone Apr 22 '09 at 9:05 This answer works - got bitten by this a few weeks ago, found this fix. – Kurt Schelfthout Apr 22 '09 at 9:07 Man that's a frustrating little problem though – Matthew Rathbone Apr 22 '09 at 9:12.

Looks like simply casting the Rating field as a nullable int has fixed it. Does this look sensible to other people? Var stuff = from l in _db.

SqlLinks select new { Link = l, Rating = (int? )(from v in l. SqlLinkVotes where v.

Tag == tagId && v. VoteDate >= since select v. Vote).Sum(), NumberOfVotes = (from v in l.

SqlLinkVotes where v. Tag == tagId && v. VoteDate >= since select v.

Vote).Count(), NumberOfComments = (from v in l. SqlLinkVotes where v. Tag == tagId && v.

VoteDate >= since && v. Comment! = "" select v.

Vote).Count() }.

Yes. As my answer suggests, you could use the null-coalescing operator to get back to a non-nullable form using 0 as the default if you wanted. (I hope that would work, anyway...) – Jon Skeet Apr 22 '09 at 9:05 It does, thankfully!

I'm turning this dynamic object into a defined object just a little further down the method so I'm replacing the nulls there. – Matthew Rathbone Apr 22 '09 at 9:13.

InvalidOperationException (null value cannot be assigned to Int32). Through debugging I've seen that this comes from the Rating property of the dynamic object. When there are no SqlLinkVotes for a particular link the Sum() results in a null value, but Rating is an int, and linq to sql thinks the Sum() will result in an int, not a nullable int.

I could easily write a stored procedure to get around this, but I thought it was a good way for me to understand linq to sql more.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions