I am using
IQueryable
and list. This is my code :
<pre lang="C#">public ActionResult List(chap = false)
{
IQueryable<UserInfo> userQuery = context.UserInfoes.Include("aspnet_Users")
.Include("aspnet_Users.aspnet_Membership")
.Include("Pshycologists").OrderBy(m => m.aspnet_Users.UserName);
List<UserInfo> userInfoList = new List<UserInfo>();
UserInfo userInfoRow = new UserInfo();
var userQueryForChapter = userQuery.OrderBy(a => a.aspnet_Users.UserName);
foreach (var row in userQueryForChapter)
{
var stagesUserCompleted = (from au in db.aspnet_Users
join spu in db.StagePageUsers on new { UserId = au.UserId } equals new { UserId = (Guid)spu.UserId }
join sp in db.StagePages on new { StagePageId = (Int32)spu.StagePageId } equals new { StagePageId = sp.StagepageId }
join s in db.Stages on new { StageID = (Int32)sp.StageID } equals new { StageID = s.StageId }
where
au.UserId == row.UserId
select s
).ToList();
if (stages.Count == stagesUserCompleted.Count)
{
userInfoRow = row;
}
userInfoList.Add(userInfoRow);
}
var users = userInfoList.ToPagedList(page, 100);
ViewBag.searchKey = user;
return View(users);
}
In using this code, I am getting error in view. The error is "
Object reference not set to an instance of an object.
. The error is on this line
@item.aspnet_Users.UserName
I guess the cause of the bug is because I am converting from
IQueryable<UserInfo>
to
List<UserInfo>
. This resulted to loss in value of aspnet_Users.UserName. How can I convert to list and still not loss value of aspnet_Users.UserName ?? I debugged and found out that aspnet_Users.UserName is null on line var users = userInfoList.ToPagedList(page, 100); (value of userInfoList.aspnet_Users.UserName is null where as value is there in userQuery.aspnet_Users.UserName ).
Any help??
What I have tried:
I added code var users = userInfoList.ToPagedList(page, 100).AsQueryable(); to make it as queryable but I got error "The model item passed into the dictionary is of type 'System.Linq.EnumerableQuery`1[Kats.DAL.UserInfo]', but this dictionary requires a model item of type 'PagedList.IPagedList`1[Kats.DAL.UserInfo]'."