Why is my element variable always null in this foreach loop?
- by ZeroDivide
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