Alias in Linq in the Select new

3
 var query1 = from ra in contexto.RADICACIONES
                 join us in contexto.USUARIOS on ra.ID_USUARIOS_FK equals us.ID_USUARIOS
                 join usex in contexto.USUARIO_EXTERNO on ra.ID_USUARIO_EXT_FK equals usex.ID_USUARIO_EXT
                 orderby ra.ID_RADICACION descending
                 select new {ra.ID_RADICACION, ra.ASUNTO, ra.FECHA, us.NOMBRE, usex.NOMBRE1 };
    gvradica.DataSource = query1.ToList();
    gvradica.DataBind();

I have this code and I want to alias the names I've tried in the following ways

 select new {Id = ra.ID_RADICACION, Asunto = ra.ASUNTO, Fecha = ra.FECHA, Enviado por = us.NOMBRE, Dirigido a = usex.NOMBRE1 };

And I've also tried like this

 select new {["Id"] = ra.ID_RADICACION,["Asunto"] = ra.ASUNTO,["Fecha"] = ra.FECHA, ["Enviado por:"]  = us.NOMBRE, ["Dirigido a:"] = usex.NOMBRE1 };

And throws the following error

The fields are generated automatically and passed to a GridView, they must be this way, since an important part of the business logic is applied there, so I can not remove the check from self-generated fields

    
asked by Ledferoz10 18.04.2017 в 15:49
source

2 answers

0

You could solve your problem with the word let inside your query. This creates a variable where you can store results.

List<string> lista = new List<string> { "uno", "dos", "tres"};
var collection = from x in lista let num1 = x[0] where num1.Equals("uno") select new { x, num1 };
    
answered by 18.04.2017 в 16:02
0

What I see, according to the error you are presenting, is that you may have problems with the null data that the query is returning to you (especially the DATE field, which is the problem).

So you should try this way:

select new {Id = (ra.ID_RADICACION ?? 0), Asunto = (ra.ASUNTO ?? ""), Fecha = (ra.FECHA ?? DateTime.MinValue), Enviado por = (us.NOMBRE ?? ""), Dirigido a = (usex.NOMBRE1 ?? "") };

EDITING:

According to your comment, then it seems that you have problems with the properties configured in GridView , In some property of GridView you have set the value ID_RADICACION (or that functionality of the business logic that you comment on your question, is establishing this value), that by changing the name obviously no longer exists. Check this well because the error starts with the word DataBinding which is the event that is thrown when the GridView is trying to establish the data within the control.

Remember that Linq loads the data so Lazy , this means that the query is only executed until it is really needed. This will serve to rule out where the problem lies, if it is the query of LINQ or if it is the GridView .

Update your code as follows:

var query1 = from ra in contexto.RADICACIONES
             join us in contexto.USUARIOS on ra.ID_USUARIOS_FK equals us.ID_USUARIOS
             join usex in contexto.USUARIO_EXTERNO on ra.ID_USUARIO_EXT_FK equals usex.ID_USUARIO_EXT
             orderby ra.ID_RADICACION descending
             select new {Id = ra.ID_RADICACION, Asunto = ra.ASUNTO, Fecha = ra.FECHA, Enviado por = us.NOMBRE, Dirigido a = usex.NOMBRE1 };

var pruebas = query1.ToList();
gvradica.DataSource = pruebas;
gvradica.DataBind();

If your execution passes the line var pruebas = query1.ToList(); means that the problem is the GridView , without your execution this line does not pass then the problem is the query LINQ .

    
answered by 18.04.2017 в 16:34