Select with Entity does not work

Asked

Viewed 62 times

0

I have this method that makes me a select but it does not return me anything.

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public JsonResult GetContratos()
{
     List<object> resultado = new List<object>();
     var contratos = db.Contratos.Where(c => c.Tipo != "N").Select(c => c.Id);

     foreach (string contrato in contratos)
     {
          resultado.Add(new { id = contrato, value = contrato });
     }
     return Json(resultado, JsonRequestBehavior.AllowGet);
}

I already tried to put the . Tolist() at the end and nothing

The SQL that is returned to me in the variable var contratos, when I run it on sql server works perfectly, it brings me back given, but on c# it brings me nothing.

{SELECT 
    1 AS [C1], 
    [Extent1].[Id] AS [Id], 
    [Extent1].[DtCriacao] AS [DtCriacao], 
    [Extent1].[DtInicioValidade] AS [DtInicioValSELECT 
    [Extent1].[Id] AS [Id]
    FROM [dbo].[Contrato] AS [Extent1]
    WHERE  NOT ((N'N' = [Extent1].[Tipo]) AND ([Extent1].[Tipo] IS NOT NULL))

Follow the image of the code being executed in the databaseinserir a descrição da imagem aqui

  • var contrtatos is null?

  • It is not null, that is the sql that comes from it

  • What happens if you remove Where : var contratos = db.Contratos.Select(c => c.Id); ? Are you sure you are connected to the correct base?

  • I have tried to take also, and yes is connected on the correct base

  • Nothing happens when I take off the Where

  • Yes, it is, because it already has a login system in place and some other things that depend on the bank

  • @Williamjohnadamtrindade can help me?

  • Start with the simplest step. Return the complete list: var contratos = db.Contratos;

  • Nor is this returning anything

  • Are you sure that Entity is connecting on the same basis that you are doing the query on? E.g.: you run the Query manually in the Sqlserver homologation, but your application is pointing to a Localdb or other database.

Show 5 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.