Why is my element variable always null in this foreach loop?

Posted by ZeroDivide on Stack Overflow See other posts from Stack Overflow or by ZeroDivide
Published on 2012-03-22T17:07:45Z Indexed on 2012/03/22 17:31 UTC
Read the original article Hit count: 219

Here is the code:

public IEnumerable<UserSummary> getUserSummaryList()
{
    var db = new entityContext();
    List<UserSummary> model = new List<UserSummary>();

    List<aspnet_Users> users = (from user in db.aspnet_Users
                 select user).ToList<aspnet_Users>();
    foreach (aspnet_Users u in users) //u is always null while users is a list that contains 4 objects
    {
        model.Add(new UserSummary()
        {
            UserName = u.UserName,
            Email = u.aspnet_Membership.Email,
            Role = Roles.GetRolesForUser(u.UserName).First(),
            AdCompany = u.AD_COMPANIES.ad_company_name != null ? u.AD_COMPANIES.ad_company_name : "Not an Advertiser",
            EmployeeName = u.EMPLOYEE.emp_name != null ? u.EMPLOYEE.emp_name : "Not an Employee"
        });
    }

    return model;
}

For some reason the u variable in the foreach loop is always null. I've stepped through the code and the users collection is always populated. The table entity for db.aspnet_Users is the users table that comes with asp.net membership services. I've only added a couple associations to it.

edit : image of debugger

debugger

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET