有条件的 EntityFramework

Conditional where EntityFramework

给定经典 ASP 中的以下 SQL 语句,您将如何翻译成 Linq?

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)"

我的问题尤其在于 if 中的位置以及它们的添加方式

基本上在 entity framework 你可以先构建查询

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

然后您可以更新查询

if(!string.IsNullOrEmpty(lastName)){
     query = query.Where(u => u.LastName == lastName);
}
return query.ToList();