ninjanye / SearchExtensions

Library of IQueryable extension methods to perform searching
MIT License
330 stars 52 forks source link

Chaining SearchChildren? #24

Open LordMike opened 6 years ago

LordMike commented 6 years ago

Hi,

Just discovered this library. I was way down a path of Expression magic when a colleague pointed me here. So far, this is awesome :).

I'm now trying to find all Persons in my DB (EF Core), where a certain relationship has a number of conditions met. My relationship is an EAV model, so the relation stores many records in the form of "Name=Value" pairs.

I tried this.

db.Persons.SearchChildren(x => x.EAV)
  .With(x=>x.Name).EqualTo("SomeProperty")
  .With(x=>x.Value).Contains("a", "b");

Which works. But chaining it like this, does not. It simply replaces the final query with just the last SearchChildren call:

db.Persons.SearchChildren(x => x.EAV)
  .With(x=>x.Name).EqualTo("SomeProperty")
  .With(x=>x.Value).Contains("a", "b")
.SearchChildren(x => x.EAV)
  .With(x=>x.Name).EqualTo("OtherProperty")
  .With(x=>x.Value).Contains("b", "c");

Should this be possible, if so, what am I missing?

ninjanye commented 6 years ago

Hi @LordMike

Thanks for getting in touch. This should indeed be possible, in the interim, you could .ToList() the first part an then perform the second, however this could be heavy on memory if you have a lot of records (which I'm guessing you being EAV)

If you fancy it, I'd be happy to accept a PR that adresses this, otherwise I'll try and look into this, this week.

Thanks again

LordMike commented 6 years ago

I'm unsure where I would start. I could only observe that the last SearchChildren seemed to take precedence.

I've worked around the issue by performing a number of regular Search'es on the EAV data, extracting unique ID references, and then searching in my source data as usual but filtering to the list of ID's.. I only have a few thousand records (tens of thousands EAV records), so this works.

I'd prefer a proper SearchChildren though :).

Also. I've added your library to https://github.com/thangchung/awesome-dotnet-core