Entity framework 获取对象引用未设置为对象的实例。来自Linq的错误

Entity framework 获取对象引用未设置为对象的实例。来自Linq的错误,entity-framework,linq,Entity Framework,Linq,我获取的对象引用未设置为对象的实例。错误 这是我的密码: public ActionResult Profile(string id) { News_Application.NewsDatabaseEntities db = new News_Application.NewsDatabaseEntities(); var result = (from u in db.AspNetUsers

我获取的对象引用未设置为对象的实例。错误

这是我的密码:

    public ActionResult Profile(string id)
    {

            News_Application.NewsDatabaseEntities db = new News_Application.NewsDatabaseEntities();
            var result = (from u in db.AspNetUsers
                          where u.Id == id
                          select new { u.Id, u.WriterName, u.ProfilePicture }).FirstOrDefault();

            UserViewModel mo = new UserViewModel();

              mo.id = result.Id;
              mo.WriterName = result.WriterName;
              mo.WriterImage = result.ProfilePicture;


              return View(mo);
    }

请帮帮我。非常感谢

我认为您的LINQ查询为您的where条件提供空值。但是您正在尝试访问
结果的属性值,而不检查它是否为null

public ActionResult Profile(string id)
{
    var db = new News_Application.NewsDatabaseEntities();
    var result = (from u in db.AspNetUsers
                          where u.Id == id
                     select new { u.Id, u.WriterName, u.ProfilePicture }).FirstOrDefault();

    if(result==null)
    {
      return Content("No records matching the where condition");
     //to do : Change to a Not found view.
    }
    UserViewModel mo = new UserViewModel();

     mo.id = result.Id;
     mo.WriterName = result.WriterName;
     mo.WriterImage = result.ProfilePicture;

     return View(mo);
}
您还可以将结果投影到UserViewModel,并使其更简单/简短

var users=db.AspNetUsers.Where(s=>sId==id)
                 .Select(x=> new UserViewModel { Id=x.Id,
                                                 WriterImage =x.ProfileImage, 
                                                 WriterName=x.WriteName}).ToList();
 if(users.Any())
 {
   return View(users.First());
 }
 return Content("User not found");

我认为你的LINQ查询给你的where条件是空的。但是您正在尝试访问
结果的属性值,而不检查它是否为null

public ActionResult Profile(string id)
{
    var db = new News_Application.NewsDatabaseEntities();
    var result = (from u in db.AspNetUsers
                          where u.Id == id
                     select new { u.Id, u.WriterName, u.ProfilePicture }).FirstOrDefault();

    if(result==null)
    {
      return Content("No records matching the where condition");
     //to do : Change to a Not found view.
    }
    UserViewModel mo = new UserViewModel();

     mo.id = result.Id;
     mo.WriterName = result.WriterName;
     mo.WriterImage = result.ProfilePicture;

     return View(mo);
}
您还可以将结果投影到UserViewModel,并使其更简单/简短

var users=db.AspNetUsers.Where(s=>sId==id)
                 .Select(x=> new UserViewModel { Id=x.Id,
                                                 WriterImage =x.ProfileImage, 
                                                 WriterName=x.WriteName}).ToList();
 if(users.Any())
 {
   return View(users.First());
 }
 return Content("User not found");

哪里堆栈跟踪是什么?我注意到您使用的是
FirstOrDefault()
,这意味着如果没有匹配项,
result
将为
null
。。。但你在无条件地取消对它的引用…在哪里?堆栈跟踪是什么?我注意到您使用的是
FirstOrDefault()
,这意味着如果没有匹配项,
result
将为
null
。。。但您正在无条件地取消引用它…我尝试了您的代码,并且在我将if(result==null)对象引用设置为对象的实例之前,它告诉我没有与where条件匹配的记录,而在我将if(result==null)对象引用设置为对象的实例之前,相同的代码告诉我。。。。问题是如何再次避免这个问题?谢谢你,所以muchI尝试了你的代码,在我放入if(result==null)后它仍然工作,但它没有告诉我没有与where条件匹配的记录,而在我放入if(result==null)对象引用之前,相同的代码告诉我没有将if(result==null)对象引用设置为对象的实例。。。。问题是如何再次避免这个问题?多谢各位