Click here to Skip to main content
15,896,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using
C#
IQueryable
and list. This is my code :
C#
<pre lang="C#">public ActionResult List(chap = false)
 {

C#
IQueryable<UserInfo> userQuery = context.UserInfoes.Include("aspnet_Users")
                                   .Include("aspnet_Users.aspnet_Membership")
                                   .Include("Pshycologists").OrderBy(m => m.aspnet_Users.UserName);


C#
List<UserInfo> userInfoList = new List<UserInfo>();
    UserInfo userInfoRow = new UserInfo();


C#
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 "
C#
Object reference not set to an instance of an object.
. The error is on this line
C#
@item.aspnet_Users.UserName


I guess the cause of the bug is because I am converting from
C#
IQueryable<UserInfo>

to
XML
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]'."
Posted
Updated 22-Jul-16 2:52am
Comments
Philippe Mori 22-Jul-16 9:07am    
Obviously, you should improve your debugging skill... In fact, as indicated in solution 1, it is easy to see that your code does not properly handles mismatching count. By putting a conditional breakpoint on the line where userInfoRow is added to the list and then moving the current execution line at the first line in the loop, one could find the problem in less than an minute...

1 solution

Quote:

C#
if (stages.Count == stagesUserCompleted.Count)
{
    userInfoRow = row;
}

userInfoList.Add(userInfoRow);

I suspect that's where the problem lies.

For the first row in the query, if stages.Count != stagesUserCompleted.Count, you're adding a blank UserInfo record to the list. None of the related entities will be populated, so when you try to access them, you'll get a NullReferenceException.

For subsequent rows, if stages.Count != stagesUserCompleted.Count, you're adding another copy of the previous row. That looks wrong to me.

I suspect what you meant to do was:
C#
if (stages.Count == stagesUserCompleted.Count)
{
    userInfoList.Add(row);
}
 
Share this answer
 
Comments
Codes DeCodes 24-Jul-16 0:58am    
it was just blunder.. any way thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900