【问题标题】:Conditional where EntityFramework有条件的哪里 EntityFramework
【发布时间】:2019-10-16 08:35:29
【问题描述】:

给定以下经典 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 的位置,以及它们的添加方式

【问题讨论】:

    标签: c# sql-server entity-framework linq entity-framework-6


    【解决方案1】:

    基本上在实体框架中你可以先构建查询

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

    然后你可以更新查询

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-22
      • 2012-02-17
      相关资源
      最近更新 更多