It throws System.InvalidOperationException: The value null not be assigned a member of type System.Boolean since it is a value type that cannot have the value null
Part of the answer is that I expect user.Friends to be empty. It is a WhereSelectEnumerableIterator returned from a linq to sql call and when it's not empty the above works fine.
this isn't a MVC discussion - I think it is a L2S or EF discussion. And maybe the count of friends is null when performing SQL translate - and therefore can not be translated ...
if (user.Friends == null || user.Friends.Count()==0)
Or add a public partial class User and add a has friends property
public bool HasFriends
{
get
{
return (Friends != null && Friends.Any())
}
}
then
if (!user.HasFriends)
Paul
http://pabloblamirez.blogspot.com - When you ask a question, remember to click "mark as answered" when you get a reply which answers your question; this ensures the right forum member gets credit below for being helpful (and makes search more relevant too).
camitz
Member
3 Points
12 Posts
Why is Enumerable.Count() throwing `InvalidOperationException` for converting `null` to `bool`?
Feb 01, 2010 07:54 PM|LINK
The following goes wrong.
if (user.Friends.Count() == 0)
....
It throws System.InvalidOperationException: The value null not be assigned a member of type System.Boolean since it is a value type that cannot have the value null
Part of the answer is that I expect user.Friends to be empty. It is a WhereSelectEnumerableIterator returned from a linq to sql call and when it's not empty the above works fine.
(Also open on stackoverflow.)
LinqToSql ASp.net-mvc
ignatandrei
All-Star
135148 Points
21679 Posts
Moderator
MVP
Re: Why is Enumerable.Count() throwing `InvalidOperationException` for converting `null` to `bool...
Feb 01, 2010 10:36 PM|LINK
this isn't a MVC discussion - I think it is a L2S or EF discussion. And maybe the count of friends is null when performing SQL translate - and therefore can not be translated ...
And maybe your Friends have a bool property ...
PaulBlamire
Participant
1272 Points
227 Posts
Re: Why is Enumerable.Count() throwing `InvalidOperationException` for converting `null` to `bool...
Feb 01, 2010 11:21 PM|LINK
if (user.Friends == null || user.Friends.Count()==0)
Or add a public partial class User and add a has friends property
public bool HasFriends
{
get
{
return (Friends != null && Friends.Any())
}
}
then
if (!user.HasFriends)
Paul