2

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 guess what I'm really asking is, what's the workaround?

Community
  • 1
  • 1
Martin
  • 2,956
  • 7
  • 30
  • 59
  • Friends implements IEnumerable, but what is its actual type? Is it a list, a query object, a custom class, ...? – Mark Byers Feb 01 '10 at 14:43
  • 1
    if you add the line of code `int count = user.Friends.Count();` above that `if` statment do you get a runtime error, or does `count` get set to something? (what?) – Richard Ev Feb 01 '10 at 14:46
  • Did you confirm that both lists have values? – johnnywhoop Feb 01 '10 at 15:08
  • @jw That's what I'm trying to do. Which is the other list? – Martin Feb 01 '10 at 15:15
  • @Mark excuse me, it's actually a WhereSelectEnumerableIterator. It's not null. @Richard Produces the same exception. See update I'm about to write. – Martin Feb 01 '10 at 15:25

5 Answers5

9

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.

Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
  • This is as far as it goes: public IEnumerable Friends { get { var friends = from u2g in FriendUserGroup.User2GroupLinks select u2g.User; return friends; } } – Martin Feb 01 '10 at 20:25
3

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.

Douglas Leeder
  • 52,368
  • 9
  • 94
  • 137
2

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!

gkrogers
  • 8,126
  • 3
  • 29
  • 36
Martin
  • 2,956
  • 7
  • 30
  • 59
0

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;
        }
    }
Blade3
  • 4,140
  • 13
  • 41
  • 57
  • We've established that it's a WhereSelectEnumerableIterator and that probably there's a failing somewhere within WhereSelectEnumerableIterator.Count(). – Martin Feb 01 '10 at 16:38
-2

If it's a list, then Count() appears to be wrong, that would be a property i.e. Count? Can you confirm this?

t0mm13b
  • 34,087
  • 8
  • 78
  • 110
  • 2
    First, he said "`Friends` is an `IEnumerble`." Second, either way, `List` is an `IEnumerable` and therefore there is an extension method `Enumerable.Count.` Lastly, he is having a runtime issue, not a compile-time issue. – jason Feb 01 '10 at 14:52