LINQ查询空引用异常 [英] LINQ query null reference exception

查看:159
本文介绍了LINQ查询空引用异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下一个查询:

  var bPermisos = from b in ruc.Permisos
                      where b.IdUsuario == cu.Id
                          select new Permisos(){
                              Id=b.Id,
                              IdUsuario=b.Id,
                              IdPerfil=b.Id,
                              Estatus=b.Estatus
                          };
  var qSisPer = from perm in bPermisos
                      **select new {                    
                          perm.IdPerfil,
                          perm.Cat_Perfil.Nivel,
                          perm.Cat_Perfil.Nombre,   
                          Nombre_Sistem=perm.Cat_Perfil.Cat_Sistema.Nombre**
                      };

并且正在抛出一个异常,请帮助!

And is throwing me an exception, plz help!

推荐答案

对于初学者,我认为第一个查询可能可能被重写为:

For starters, I think the first query can probably be rewritten as:

var bPermisos = ruc.Permisos.Where(b => b.IdUsuario == cu.Id);

除此之外,您的代码正在做什么还不清楚您似乎正在重新投影您已有的结果 - 使用已知类型的项目并创建匿名类型来保存它们。此外,第二个投影正在访问在第一个查询中未选择的一群成员。

Beyond that, it is rather unclear what your code is doing. You appear to be re-projecting the results you already have -- taking items of a known type and creating an anonymous type to hold them. Furthermore, the second projection is accessing a bunch of members that were not selected in the first query.

这篇关于LINQ查询空引用异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆