Doubt about performance in using the statement

Asked

Viewed 82 times

3

Which of the two ways to verify if a record of such TYPE has ever been registered is more performative? Are there differences, errors? What are the implications? The block using frees only connection resources or all objects created in its scope? The memory used by using has priority in GC? There is another better way to do such a check?

Block 1

int valorDoTipo = 5;
Tipo tipo;

using (Contexto contexto = new Contexto())
{
    tipo = contexto.Tipo.FirstOrDefault(t => t.ValorDoTipo == valorDoTipo);
}

if (tipo != null)
    return true;
else
    return false;

Block 2

using (Contexto contexto = new Contexto())
{
    int valorDoTipo = 5;
    Tipo tipo;        
    tipo = contexto.Tipo.FirstOrDefault(t => t.ValorDoTipo == valorDoTipo);
    if (tipo != null)
        return true;
    else
        return false;
}
  • 1

    The end (the 4 lines of the if) of your code can be written as return tipo != null;

  • yeah, the direct boolean expression, so simple... never thought of it, would be lack of experience?

  • 1

    Probably why you’re all here, learning from the experience of others. Even the less experienced help the more experienced, after all, each with their own experience.

  • 1

    @Caiquec. Entity framework is very cool and has so much that today I live learning, the most important is this and let’s say it is not lack of experience, it is a learning forever ... congratulations!

1 answer

3


It has better and more performative solutions than you gave me in your question, one of them just below:

Explanation: How you want a comeback (true ou false) would be the best way this there, need not return a Tipo as in the question.

bool retorno = false;
using (Contexto contexto = new Contexto())
{                 
    retorno = contexto.Tipo.Any(t => t.ValorDoTipo == 5);    
}
return retorno;

or

bool retorno = false;
using (Contexto contexto = new Contexto())
{                 
    retorno = contexto.Tipo.AsNoTracking().Any(t => t.ValorDoTipo == 5);    
}
return retorno;

The AsNoTracking(), means that it will execute an SQL command in your database and will not attach to your context, i.e., increases the performance of the command. Such command is widely used in searches, where the purpose is only to bring information (Readonly).

Obs: Are two equal (==) in the given comparison seen in the question tag .

References:

  • You can give me more information about the Asnotracking method()?

  • @Caiquec. I edited the question, usually we use it to optimize the search and not to cache it! no need in your scenario.

  • Got it, but here I’m not having access to this method. Would be the EF version? (I don’t know which one I’m using, but my VS is 2010)

  • You can make it shorter: return contexto.Tipo.AsNoTracking().Any(t => t.ValorDoTipo == 5);

  • @Caiquec. what did you not find? which method?

  • Asnotracking().

  • @Caiquec. use without, so also solves as it is now my issue of the question!

  • @Gypsy omorrisonmendez may be, but, it’s best to always put the return in the last line is good practice !!!

  • @Harrypotter ok, I will use without, but... can you tell me why I do not have access to the method? It would be the EF version, . Net? And using without, I’ll be caching the data?

  • 1

    @Caiquec. it was supposed to have, it’s hard for me to predict what’s on your screen, I’m sorry, about the cache in this case it may run out because it returns a simple primitive type it won’t have so many differences, but if you do it in data mass searches and they’re typed it makes a big difference!

  • 1

    @Harrypotter Ok, no problem. Even the system asking to avoid things like, thank you.

  • @Caiquec. try this: maybe the EF version works: contexto.Tipo.Where(t => t.ValorDoTipo == 5).AsNoTracking().Any(); Just to take the test I think you’re messing with the 4.

  • 1

    @Harrypotter didn’t work... I’ll do some research, internship is for that right rs

Show 8 more comments

Browser other questions tagged

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