What is the best solution ? Getbyid with a non-existent id in the BD

Asked

Viewed 96 times

5

I’m making a call in my Pository but CPF does not always exist in the BD. When it does not exist in the database I get the following error.

inserir a descrição da imagem aqui

Repository

public VendedorModel GetById(int CPF)
    {
        using (var db = new PowerVendasEntities())
        {
            var registro = db.Vendedor.Single(x => x.VendedorCPF == CPF);

            return VendedorFactory.ToModel(registro);
        }
    }

How best to solve this problem ?

4 answers

3


You can use Firstordefault() or the Singleordefault() instead of Single().

Firstordefault():

Returns the first element of a sequence, or a default value if a sequence does not contain elements.

Singleordefault()

Returns the single element of a sequence or a default value if a sequence is empty; this method generates an exception if there is more than one element in the sequence.

How would it look:

public VendedorModel GetById(int CPF)
    {
        using (var db = new PowerVendasEntities())
        {
            var registro = db.Vendedor.FirstOrDefault(x => x.VendedorCPF == CPF);

            return VendedorFactory.ToModel(registro);
        }
    }
  • but I can’t do the "Return Vendedorfactory.Tomodel" pq registration will be null. Some alternative ?

  • 1

    @carloshenriqueribeiro Return null =D

  • Placing a conditional ? pq the parameter can be a valid CPF and found in the base, in this case you have to return the same model.

  • I didn’t understand much of what you meant, but as LINQ said you can return null and treat that where you called that function (Getbyid(CPF)), informing the user that CPF does not exist in the bank.

2

You can use the following:

public VendedorModel GetById(int CPF){
    using (var db = new PowerVendasEntities()){
         var registro = db.Vendedor.SingleOrDefault(x => x.VendedorCPF == CPF);
         return registro != null ? VendedorFactory.ToModel(registro) : null;
    }
}

Only by supplementing what has already been well explained in the accepted reply on the SingleOrDefault(), in this case if the Bank does not find any Seller with this CPF, it will return a null, using a Ternary Operator

1

A solution option can also be this:

public VendedorModel GetById(int CPF)
{
    using (var db = new PowerVendasEntities())
    {
        var registro = db.Vendedor
            .Where(x => x.VendedorCPF == CPF)
            .FirstOrDefault();

        return VendedorFactory.ToModel(registro);
    }
}

1

The problem of using the Single is that it launches into the database a Top 2, and if there are two records for the past filter it returns an error and if no record is found it also returns an error, that is, it expects only one record to be found in the database.

This is a problem because it causes performance loss, out which still has to be dealt with in the application the errors.

The solution indicated is to use the methods; First or Firstordefault. You can even use the Singleordefault, but it would still be sending a TOP 2 to the base and if there is more than one record it still returns an error.

Browser other questions tagged

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