views:

273

answers:

2

Hi! I have the next query:

  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!

A: 

This could be happening because of any of the following:

  • cu is null
  • One element in ruc.Permisos is null, causing the exception on b.IdUsuario

If it's the latter, you can just handle this by adding:

var bPermisos = from b in ruc.Permisos
                where b != null && b.IdUsuario == cu.Id
                 // ... rest of your code
Reed Copsey
did that and Is not working =S
+1  A: 

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.

Jay
Yes is reusing because Permisos is a table with foreing key of Cat_Perfil and Cat_Usuario, and Cat_Perfil has a foreign key of Cat_Sistema, what I want to do is show the columns of Cat_Perfil that are inside the query bPermisos but with instead of Ids, the names of that Ids in this case Cat_Sistema, plz tell me if I'm writing confusing.thanx!!
Okay, the problem is that when use the `select` statement, you are creating a new object of type `Permiso`, and setting the ID values. That object has no data from any of the other tables or any properties except the ones you specified. You can either pull that data with the query, and `select` the aggregates (collection properties), (or just don't use `select`), or you can write a second query *against the other tables*, passing in the IDs you got from the first query.
Jay
Ok now it brings the data, thanx! now the problem is that I can't see it, it's not binding to the datagrid, is a case of silverlight, It can be asked here?
Sure, but you should create a new question specific to that issue. You can include a link to this question for reference.
Jay