EntityFramework-Plus icon indicating copy to clipboard operation
EntityFramework-Plus copied to clipboard

nesting IncludeFilter

Open KrayZeee opened this issue 7 years ago • 4 comments

Hello everyone, i found recently this library and had high hopes. i wasnt dissapointed, it is quite a nice package.

Im sitting before a problem and wanted to use includefilter to solve it, but im kind of stuck.

lets say i have 3 or more tables which are in a one-to-many relationship. layout like this.

my goal is to find all items in Table C by a criteria and have a entity from type A given back that has only the navigation properties to the found items in C.

desired goal the red marked paths represents the entety of type A i want to get with the corresponding navigationproperties.

i have lazyloading disabled but could enable it if necessary.

i have tried a couple of ways but none of which satisfy me. The query should NOT give back all entries inside these tables, because this query will be get called quite often and the result would be to big since it grows exponentially with every related table.

a couple of ways i tried:

var Cs = new List<C>();
			using (var DataBaseContext = ContextDBGet())
			{
				DataBaseContext.Configuration.LazyLoadingEnabled = false;
				Cs= DataBaseContext.C.Where(c => c.ID == queryparameter).Include("B.A")).ToList();
			}
			var As= new List<A>();
			foreach (var c in Cs)
			{
				Aa.Add(c.B.A);
			}

this gives me back everything also creates for every found C a A

var As = new List<A>();
			using (var DataBaseContext = ContextDBGet())
			{
				DataBaseContext.Configuration.LazyLoadingEnabled = false;
				As= (from x in DataBaseContext.A
						where (from y in DataBaseContext.B
							   where (from z in DataBaseContext.C
														   where z.ID == 1
														   select z ).Any()
									  select z).Any()
							   select y).Any()
						select x).ToList();
			}

this doesnt eagerly loads the naviagtion properties. if i include("B.C") everything is given back again.

i hope with includefilter something in this direction would be possible:

DataBaseContext.A.IncludeFilter(q=>q.B.IncludeFilter(x=>x.C.Where(y=>y.ID == something))

the thing is, that this example only includes 3 tables but the problem im working with are 8 tables nested. btw im trying to avoid sending multiple queries.

i hope this is possible with IncludeFilter, else i would be really grateful for alternative solutions.

sincerely KrayZee

KrayZeee avatar Oct 07 '19 08:10 KrayZeee

Hello @KrayZeee ,

I'm not exactly sure what is the problem with your description. You explain it very well but show issue with Include and not IncludeFilter.

However, your LINQ should probably look more like this:

DataBaseContext.A
   .IncludeFilter(q=>q.B)
   .IncludeFilter(q=>q.B.Select(x=>x.C.Where(y=>y.ID == something)))

Yes, our IncludeFilter should support this kind of scenario and make only one database round-trip (send only one query).

If you know how to use this feature, it should already work:

  • Use either IncludeFilter or IncludeOptimized, both allow filtering
  • Don't mixte Include, IncludeFilter, IncludeOptimized, you start to use one method, you always use it in your query
  • Make sure to include and filter all paths

You can find some online example here: https://entityframework-plus.net/online-examples

Is there something that doesn't work or you need help with? Just send us a runnable project and we will be happy to help you.

Best Regards,

Jonathan


Performance Libraries context.BulkInsert(list, options => options.BatchSize = 1000); Entity Framework Extensions • Entity Framework Classic • Bulk Operations • Dapper Plus

Runtime Evaluation Eval.Execute("x + y", new {x = 1, y = 2}); // return 3 C# Eval Function • SQL Eval Function

JonathanMagnan avatar Oct 07 '19 10:10 JonathanMagnan

Dear Jonathan, thank you for your quick reply,

the thing is im just not getting the result im expecting and i already have tried so many things that i started to confuse myself.

the snippet you provided includes all Bs where one of them then contains the C, also it loads all As with theier Bs even if no Cs are found.

i want to disregard everything that doesnt lie on the path to found C

to visualize the difference between actual and desired lets say i want all C with id 1

DataBaseContext.A
   .IncludeFilter(q=>q.B)
   .IncludeFilter(q=>q.B.Select(x=>x.C.Where(y=>y.ID == 1)))

is giving me provided solution

but what my goal would look like this: where1

sincerely KrayZee

KrayZeee avatar Oct 07 '19 11:10 KrayZeee

Understood,

I probably have many syntax errors, but basically, you want something like this if I understand:

DataBaseContext.A
   .IncludeFilter(a=>a.B.Any(b => b.C.Any(c => c.ID == 1)))
   .IncludeFilter(a=>a.B.Any(b => b.C.Any(c => c.ID == 1)).Select(b=>b.C.Where(y=>y.ID == 1))))
   .Where(a => a.B.Any(b => b.C.Any(c => c.ID == 1)));

As you see, it's possible but it will very fast start to be very complicated when using with 8 tables. I don't think that's a really viable long term solution for you.


Do you have a reference from child to parent? Inversing the query could be easier to understand

var result = DatabaseContext.C.Where(c => c.ID == 1).Include("C.B.A").ToList()
var listA1 = DataBaseContext.A.Local.ToList(); // if the context is used only for this query
var listA2 = result.SelectMany(c => c.B.Select(b => b.C)); // just do the path in the inverse

JonathanMagnan avatar Oct 07 '19 15:10 JonathanMagnan

wow this is really a mess with 8 tables and definitly not a viable solution.

yes i have references from child to parent, first example from me used a "buttom-up"-query. i didnt know of the Local property, this instantly solved my problem. thank you veeeeery much.

var test = new List<A>();
			using (var DataBaseContext = ContextDBGet())
			{
				DataBaseContext.Configuration.LazyLoadingEnabled = false;
				DataBaseContext.Configuration.ProxyCreationEnabled = false;
				var result = DataBaseContext.G.Where(c => c.ID == 1 || c.ID == 1).Include(x => x.F.E.D.C.B.A).ToList();
				test = DataBaseContext.A.Local.ToList();
			}

sincerely KrayZee

KrayZeee avatar Oct 07 '19 15:10 KrayZeee