Why is Enumerable.Count() throwing `InvalidOperationException` for converting `null` to `bool`?
Can anyone explain how on earth the following statement can generate
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 (freely translated from Swedish (see also)).
if (user.Friends.Count() == 0)
user is a User and Friends is an IEnumerable<User>
.
Update: Friends is returned from a linq to sql call and is actually a WhereSelectEnumerableIterator
. It is empty in this case so I had expected the above to evaluate to true. In cases when Friends is not empty this works fine. So for some reason when it's empty havoc descends, it'd be nice to know why, but I gues开发者_Python百科s what I'm really asking is, what's the workaround?
user.Friends
is a lazy-evaluated enumeration (yield return
or an Linq query), and something inside the evaluation is throwing the exception. Without more code it's impossible to say exactly where.
Split up the code line, and see what error is reported:
int temp = user.Friends.Count();
if (temp == 0)
Then split up the first line if the problem is still unclear.
As I wrote in a comment to 280Z28, the getter looks like this.
public IEnumerable<User> Friends
{
get
{
var friends = from u2g in FriendUserGroup.User2GroupLinks
select u2g.User;
return friends;
}
}
FriendUserGroup.User2GroupLinks is an EntitySet. Its Count property is what throws the exception. Any() will also, in case anybody was wondering. What you need to do instead is to check HasLoadedOrAssignedValues. So I inserted
if (!FriendUserGroup.User2GroupLinks.HasLoadedOrAssignedValues)
return null;
in the getter above.
I hate answering my own question when other people have put in effort. Thanks for all the input!
The Friends IEnumerable appears to have not been initialized. For example, the snippet below does not give a runtime error, because in Main(), I set Friends to a generic list.
public sealed class Program
{
public static void Main()
{
var user = new User
{
Friends = new List<Int32>()
};
if (user.Friends.Count() == 0)
{
// TODO: Input logic here
}
}
}
/// <summary>
/// User class
/// </summary>
public class User
{
/// <summary>
/// Gets or sets the friends.
/// </summary>
/// <value>The friends.</value>
public IEnumerable<Int32> Friends
{
get; set;
}
}
If it's a list, then Count()
appears to be wrong, that would be a property i.e. Count
? Can you confirm this?
精彩评论