none
.First() returns the only item in a list, but .ToList() complains about the same list being null RRS feed

  • Question

  • Hello all,

    Hoping someone can help, though I feel I may be missing something obvious.

    Using Entity Framework 6.x, I have a Role class:

    public partial class Role
        {
            public int RoleId { get; set; }
    
            public RoleName RoleName { get; set; }
    
            public virtual Terminal Terminal { get; set; }
    
            public virtual User User { get; set; }
    
            public Role()
            {
                Terminal = null;
                User = null;
            }
        }
    

    RoleName is an enum, Terminal and User are optional, though each role would have one or the other assigned. Perhaps that's part of my problem.

    My linq query looks like this, providing an int userId :

    var roles = from r in db.Roles.Include("User") where (r.User.UserId == userId) select r;            
    if (roles != null)
    {
        Role t = roles.First();
        RoleName n = t.RoleName;
        var tempRoles = roles.ToList();
        if (tempRoles.Count > 0)
            return tempRoles;
        }                
        return null;
    }

    Its not the best as I've been experimenting. But, the instruction roles.First() returns a Role object and its User object.

    roles.ToList() gives the error "Object reference not set to an instance of an object". If I debug it line by line, and view the roles object, that has the same error on enumeration.

    How can ToList() complain that roles is null, and First() return an object?

    Any suggestions appreciated.

    Cheers,

    Mike

    Wednesday, October 15, 2014 11:39 AM

All replies

  • Hello, I would suggest that you first change the line

    if (roles != null)

    to

    if (roles.Count() > 0)

    See if you still come inside the if-statement.

    Best regards,

    Thorsten

    Wednesday, October 15, 2014 12:05 PM
  • Hi Thorsten,

    Thanks for the reply. I just changed it, and I get the NullReferenceException again, at that very line, so same fault.

    Cheers,

    Mike

    Wednesday, October 15, 2014 2:08 PM
  • Hello Mike,

    >>How can ToList() complain that roles is null, and First() return an object?

    It really sounds strangely. It seems that you are using Entity Framework Code First approach, if I understand correct, could you please share more detail code with us as providing the code about the RoleName enumeration, the Terminal and User classes, also we need to know the relationship between these entities as one to one or one to many.

    And here are some links I think might be helpful:

    Enum Support in Code First, this would tell how we could use the enumeration in Code Fisrt.

    Configuring Relationships with the Fluent API, this would tell how we could configure various relationships in Code First.

    If thses links does not help you, please provide information I mentioned so that we could make a test to know the caused reason.

    Best Regards,

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, October 16, 2014 2:39 AM
    Moderator