使用list< T>时如何防止数据丢失数据?其来源是iqueryable< T>类型。 [英] How do I prevent data loss data while using list<T> whose source is of type iqueryable<T>
问题描述
我正在使用
IQueryable
和列表。这是我的代码:
< pre lang = C#>公共ActionResult列表(chap = false )
{
IQueryable< UserInfo> userQuery = context.UserInfoes.Include( aspnet_Users)
.Include( aspnet_Users.aspnet_Membership)
.Include( Pshycologists)。OrderBy(m = > m.aspnet_Users.UserName);
列表< UserInfo> userInfoList = new List< UserInfo>();
UserInfo userInfoRow = new UserInfo();
< pre lang =C#> var userQueryForChapter = userQuery.OrderBy(a = > a.aspnet_Users。用户名);
foreach ( var 行 userQueryForChapter)
{
var stagesUserCompleted =( from au db.aspnet_Users
join spu in db .stagePageUsers on new {UserId = au.UserId}等于 new {UserId =(Guid)spu.UserId }
join sp db.StagePages new {StagePageId =( Int32 )spu.StagePageId}等于 new {StagePageId = sp.StagepageId}
join s db.Stages on new {StageID =( Int32 )sp.StageID}等于 new {StageID = s.StageId}
其中
au.UserId == row.UserId
选择 s
).ToList();
if (stages.Count == stagesUserCompleted.Count)
{
userInfoRow = row;
}
userInfoList.Add(userInfoRow);
}
var users = userInfoList.ToPagedList(page,100);
ViewBag.searchKey = user;
返回查看(用户);
}
使用此代码时,我收到错误。错误是
对象引用而不是 set 到<的实例< span class =code-keyword> object 。
。错误在这一行
@ item.aspnet_Users。用户名
我想这个错误的原因是因为我正在转换自
IQueryable< UserInfo>
到
列表< UserInfo >
。这导致aspnet_Users.UserName的值丢失。如何转换为列表仍然没有丢失aspnet_Users.UserName的值我调试并发现aspnet_Users.UserName在行var user = userInfoList.ToPagedList(page,100)上为null;(userInfoList.aspnet_Users.UserName的值为null,其中userQuery中有值。 aspnet_Users.UserName)。
有什么帮助吗? ?
我尝试了什么:
我添加了代码var用户= userInfoList.ToPagedList(page,100).AsQueryable();使它成为可查询但我得到错误传入字典的模型项是'System.Linq.EnumerableQuery`1 [Kats.DAL.UserInfo]'类型,但这个字典需要一个类型为'PagedList.IPagedList的模型项`1 [Kats.DAL.UserInfo]'。
Quote:
if (stages.Count == stagesUserCompleted.Count)
{
userInfoRow = row;
}
userInfoList.Add(userInfoRow);
我怀疑这就是问题所在。
对于查询的第一行,如果stages.Count!= stagesUserCompleted.Count
,你就是将空白UserInfo
记录添加到列表中。没有任何相关实体将被填充,因此当您尝试访问它们时,您将获得NullReferenceException
。
对于后续行,如果stages.Count!= stagesUserCompleted.Count
,则表示您要添加上一行的另一个副本。这对我来说是不对的。
我怀疑你打算做的是:
if (stages.Count == stagesUserCompleted.Count)
{
userInfoList.Add(row);
}
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]'."
Quote:
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, ifstages.Count != stagesUserCompleted.Count
, you're adding a blankUserInfo
record to the list. None of the related entities will be populated, so when you try to access them, you'll get aNullReferenceException
.
For subsequent rows, ifstages.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:
if (stages.Count == stagesUserCompleted.Count) { userInfoList.Add(row); }
这篇关于使用list< T>时如何防止数据丢失数据?其来源是iqueryable< T>类型。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!