Where does not work when listing all records - Entity Framework

0

I need to return all records that contain the informed branch using the where clause, but the Entity Framework is returning all records ignoring what is in where . Where am I going wrong?

public IQueryable<PessoaGenerico> GetAllPessoaGenericoByFilial(int id)
{
  return DbSet
   .Where(pg => pg.PessoaFilialId == id)
   .AsQueryable()
   .AsNoTracking();
}

Intheexample,I'mlookingforallrecordswherePessoaFilialIdisequalto31.Itshouldnotfindandreturnnull,butit'sallfetching.

    
asked by anonymous 19.08.2018 / 22:53

1 answer

0

The ToList() was missing. It turns out that IQueryable it just creates the query, it will actually be executed when you "indicate" this with ToList() or FirstOrDefault() , for example.

To check for value, you can use the Any() method, which will return true if it has value, eg:

if (suaLista.Any()) {
    //Possui valor
}

Suggested reading: What's the difference between IEnumerable, IQueryable, and List?

Maybe someone else can explain better and in more detail, but in short that's it.

    
20.08.2018 / 00:36