Where it doesn’t work when listing all records - Entity Framework

Asked

Viewed 65 times

0

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

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

inserir a descrição da imagem aqui

In the example, I’m searching all the records where PessoaFilialId is equal to 31. He should not find and return null, but it’s bringing all.

inserir a descrição da imagem aqui

1 answer

0


Missed the ToList(). It turns out that the IQueryable it just creates the query, it will actually run when you "indicate" it with the ToList() or a FirstOrDefault(), for example.

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

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

Reading suggestion: What is the difference between Ienumerable, Iqueryable and List?

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

  • Thanks for the @Barbetta return, but it didn’t work with Tolist(). It keeps bringing all the records.... strange this...

  • @Jalberromano that weird, I don’t see anything different in the code anymore.. Let’s do some rsrs tests, try it like this public List<SuaClasse> GetAllPessoaGenericoByFilial(int id)&#xA; {&#xA; return DbSet.Where(pg => pg.PessoaFilialId == id).AsNoTracking().ToList();&#xA; } changing the method directly. DbSet where you form him?

  • I think I figured out the problem... I was validating inside an if (with my repository function and the return was not null... The return is coming (Empty = "Enumeration yielded in Results")... Why in some cases the return is null and in other Empty? I’ll take care of Empty?

  • Pera, let’s separate the cases, there in the print your are returning value, after using the ToList() stopped coming value? or even with the IQueryable is returning Empty?

  • No. It’s just that I was validating like this: "if ( _personGenericoRepository.Getallpersongenericobyfilial(personGenerico.Pessoaid) != null)". When I "debbugava" and positioned the mouse over personally EnericoRepository, all records were returned (with or without Tolist), but it was because the function was within If. Now I’ve validated it like this: "var test = _personGenericoRepository.Getallpersongenericobyfilial(personGenerico.Pessoaid); if (test != null)" and the return is Empty , because Ienumerable and Iquerable returns Empty when not found... I just need to treat when I’m Empty....

  • In short: I need to treat the return "Empty", that is, if the return is different from Empty....

  • I edited the answer with the one If(list.Any()),tries it to see if it will work

  • It worked 100%!!!! Thanks Bro!!!

Show 3 more comments

Browser other questions tagged

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