Conditional where EntityFramework

c# entity-framework entity-framework-6 linq sql-server

Question

Given the following SQL statement in classic ASP, what would your translation into Linq be like?

sq = " select cod_troquel, referencia, fila, columna, dimX, dimY, ancho, avance, calleVertical, calleHorizontal, pinzaIzquierda, pinzaDerecha, tt.nombre, ett.nombre, pinzaSuperior, pinzaInferior,radioCantos, convert(tinyint, t.regular), fichero, t.observaciones, t.nEjemplares, t.aplicarPrecioGolpe, t.golpes, app.tipoPagina "&_
     " from agrupacionProductoProducto app "&_
     " inner join troquelAPP tapp on tapp.agrupacionProductoProducto=app.cod_agrupacionProductoProducto "&_
     " inner join etTroquel t on t.cod_troquel=tapp.troquel "&_
     " left join etTipoTroquel ett on ett.cod_etTipoTroquel = t.tipoTroquel2 "&_
     " left join tipoTroquel tt on tt.cod_tipoTroquel = t.tipoTroquel "&_
     " where t.usoArchivo=1 and t.ideal=0 and t.baja=0 and app.cod_agrupacionProductoProducto="&codigo

if tipoPagina=1 then 'LAMINA SIMPLE
    sq = sq & " and t.regular = 0"
else 'ETIQUETA
    sq = sq & " and t.regular = 1"
end if

sq = sq & " order by upper(referencia)"

My problem in particular is with the where there are in the if, in how they are added

1
0
10/16/2019 8:35:29 AM

Accepted Answer

basically in entity framework you can build firstly query

var query = _dbContext.Users.Where(u => u.Name == "Josh");

then you can update query

if(!string.IsNullOrEmpty(lastName)){
     query = query.Where(u => u.LastName == lastName);
}
return query.ToList();
2
10/16/2019 8:51:55 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow