Select in Entity Framework is not working

Asked

Viewed 233 times

0

I am performing a consultation using LINQ, very simple query I am using the following method.

public void verificaAcesso( string usuario, string senha)
{
    using (DbBancoContext banco = new DbBancoContext())
    {
        var resultadoConsulta = from u in banco.USUARIOS_PROCESSO
                                where
                                   u.USUARIO == usuario
                                   && u.SENHA == senha
                                   && u.DT_CANCELAMENTO != null
                                select u.ID_USUARIO;
    }
}

Only I’m not getting any feedback but when I check the contents of the variable resultadoConsulta displays the following content.

{SELECT [Extent1].[ID_USUARIO] AS [ID_USUARIO]
 FROM [dbo].[USUARIOS_PROCESSO] AS [Extent1]
 WHERE ([Extent1].[USUARIO] = @p__linq__0) AND ([Extent1].[SENHA] = @p__linq__1) AND ([Extent1].[DT_CANCELAMENTO] IS NOT NULL)}

What am I doing wrong?

  • 4

    Hello. The query has not been made yet, it will be made only when using the result. To bring the result soon can add .ToList() at the end of the query.

  • 1

    @Omni I gave a +1 in your comment but it would be nice to answer the question. Just by your comment, no one could condemn me for trying to do ...select u.ID_USUARIO.ToList(); in the last row.

  • 1

    @Caffé was leaving work and did not give, but it’s good that answered =) upvoted

1 answer

3


Add a line:

var idsUsuarios = resultadoConsulta.ToList(); The result will be in idsUsuarios.

Or surround your query by parentheses, adding to it a call to ToList:

    var resultadoConsulta = (from u in banco.USUARIOS_PROCESSO
                            where
                               u.USUARIO == usuario
                               && u.SENHA == senha
                               && u.DT_CANCELAMENTO != null
                            select u.ID_USUARIO).ToList();

and then the result will be in resultadoConsulta.

What your code does so far is just create the query and not run it.

  • Your help was good but there is a small problem in my case when I check the value of the result it gives me a Count = 0 would not be error in my connectionstring

  • @Rabelos The only mistake I can imagine in connectionstring is that it is pointing to the wrong base, otherwise you would have an exception and not an unexpected amount of records. It seems that there are simply no records that satisfy the filter. Check: uppercase and lowercase letters in the user name (the database may be case sensitive), if the password is encrypted in the database and not encrypted in the query, if the users whose Ids you intended to return were actually canceled (DT_CANCELAMENTO != null).

  • 1

    @If I were to bet, I’d say your cancellation date filter is wrong. The way it is, you only return the ID of users who "have been canceled" DT_CANCELAMENTO != null), if this is your business language. Should it not be && u.DT_CANCELAMENTO == null?

  • I noticed that my dear @Caffé distracted by the anger of not understanding uahuauahau. Thank you so much for your help.

Browser other questions tagged

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