对于Entity Framework选择SQL大小写

时间:2019-04-12 12:29:25

标签: entity-framework entity-framework-core

我有这个select可以在T-SQL中使用,但是需要转换为Entity Framework。如何在Entity Framework中使用大小写?

select 
    *
from 
    Contrato c
where 
    c.QtdParcelasFalta > 0 
    and (case
            when c.DiaEspecificoMarcar = 1 
               then c.DiaEspecifico
            when c.DiaEspecificoMarcar = 0
               then (select convert(int, substring(DataCobranca, 2, 2)) 
                     from Contrato 
                     where Contrato.Id = c.Id) - 
                    (select e.DataProcessamentoNota 
                     from Contrato t 
                     inner join PedidoVenda p on p.id = t.PedidoVendaId 
                     inner join Empresas e on p.EmpresaID = e.Id 
                     where p.id = t.PedidoVendaId 
                       and t.id = c.Id and p.EmpresaID = 1)
         end) = (SELECT DAY(GETDATE()))

我这样尝试过,但没有成功,没有传递正确的值,因为我不知道传递给Entity Framework:

var contrato = db.Contrato
                 .Include(a => a.PedidoVenda)
                 .Include(a => a.Cliente)
                 .Where(a => a.PedidoVenda.EmpresaID == model.EmpresaID 
                             && a.Cancelado == false 
                             && a.DiaEspecificoMarcar == true
                             && a.DiaEspecifico == int.Parse(DateTime.Now.ToString("dd")) ||
                        (int.Parse(a.DataCobranca.Substring(1, 2)) - a.PedidoVenda.Empresa.DataProcessamentoNota)
              == int.Parse(DateTime.Now.ToString("dd"))
              ).ToList();

1 个答案:

答案 0 :(得分:2)

您实际上是在正确的轨道上。您只需要在lef上添加when条件和正确的条件,例如:

var contrato = db.Contrato.Include(a => a.PedidoVenda).Include(a => a.Cliente).Where
                  (a => a.PedidoVenda.EmpresaID == model.EmpresaID && a.Cancelado == false &&
              a.DiaEspecificoMarcar == false && a.DiaEspecifico == int.Parse(DateTime.Now.ToString("dd")) ||
              ( a.DiaEspecificoMarcar == true && int.Parse(a.DataCobranca.Substring(1, 2)) - a.PedidoVenda.Empresa.DataProcessamentoNota)
              == int.Parse(DateTime.Now.ToString("dd"))
              ).ToList();

我还建议您使用变量中的DateTime.Now来确保两个int.Parse都获得相同的输入