Cercando di eseguire una query con la clausola where a livello foglia nel core della struttura dell'entità ma ottenendo questo errore:
Unhandled Exception: System.ArgumentException: The property expression 'c => {from Child t in c.Children where ([t].Name != "NoInc
lude") select [t]}' is not valid. The expression should represent a property access: 't => t.MyProperty'. For more information on
including related data, see http://go.microsoft.com/fwlink/?LinkID=746393.
at Microsoft.EntityFrameworkCore.Internal.ExpressionExtensions.GetComplexPropertyAccess(LambdaExpression propertyAccessExpressi
on)
at Microsoft.EntityFrameworkCore.Query.ResultOperators.Internal.ThenIncludeExpressionNode.ApplyNodeSpecificSemantics(QueryModel
queryModel, ClauseGenerationContext clauseGenerationContext)
at Remotion.Linq.Parsing.Structure.IntermediateModel.MethodCallExpressionNodeBase.Apply(QueryModel queryModel, ClauseGeneration
Context clauseGenerationContext)
at Remotion.Linq.Parsing.Structure.QueryParser.GetParsedQuery(Expression expressionTreeRoot)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](Expression query, INodeTypeProvider nod
eTypeProvider, IDatabase database, ILogger logger, Type contextType)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass19_0`1.<CompileQuery>b__0()
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
at Remotion.Linq.QueryableBase`1.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at dotnetsql.Demo.Main(String[] args) in C:\Users\asad\dev\dotnetsql\Program.cs:line 15
Ecco il codice completo che può essere utilizzato in un'applicazione console per replicare questo:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
namespace dotnetsql
{
class Demo
{
static void Main(string[] args)
{
using (var context = new FamilyContext())
{
var seed = new SeedData(context);
var result = context.GrandParent
.Include(p => p.Parents)
.ThenInclude(c => c.Children.Where(t => t.Name != "NoInclude"))
.ToList();
Console.WriteLine(result.Count);
}
}
}
public class FamilyContext : DbContext
{
public FamilyContext()
{
Database.EnsureCreated();
}
public DbSet<GrandParent> GrandParent { get; set; }
public DbSet<Parent> Parent { get; set; }
public DbSet<Child> Child { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseInMemoryDatabase();
}
}
public class GrandParent
{
public int Id { get; set; }
public string Name { get; set; }
public List<Parent> Parents { get; set; }
}
public class Parent
{
public int Id { get; set; }
public string Name { get; set; }
public int GrandParentId { get; set; }
public GrandParent GrandParent { get; set; }
public List<Child> Children { get; set; }
}
public class Child
{
public int Id { get; set; }
public string Name { get; set; }
public int ParentId { get; set; }
public Parent Parent { get; set; }
}
// Seed Data
public class SeedData
{
public SeedData(FamilyContext context)
{
if (!context.GrandParent.Any())
{
context.GrandParent.AddRange(
new List<GrandParent>
{
new GrandParent{Name = "Grandparent 1", },
new GrandParent{Name = "Grandparent 2", },
}
);
context.SaveChanges();
context.Parent.AddRange(
new List<Parent> {
new Parent{Name = "Parent 1", GrandParentId = 1},
new Parent{Name = "Parent 2", GrandParentId = 1},
}
);
context.SaveChanges();
context.Child.AddRange(
new List<Child> {
new Child{Name = "Child 1", ParentId = 1},
new Child{Name = "Child 2", ParentId = 1},
new Child{Name = "NoInclude", ParentId = 1}
}
);
context.SaveChanges();
}
}
}
}
Leggi questo articolo ma l'esempio di caricamento lento è semplice e prevede di caricare una singola entità prima di filtrare i suoi record figli che non aiutano.
Ecco l'applicazione console completa per replicare rapidamente questo.
Grazie.
Supponendo che si desideri eseguire un filtro incluso : una funzione richiesta molte volte e non supportata in nessuna versione EF, incluso Core. AFAIK c'è un piano per aggiungerlo a EF Core, ma fino ad allora devi risolverlo manualmente.
La tecnica è fondamentalmente la stessa come spiegato nella sezione Caricamento dati correlati - Caricamento esplicito , filtro che le entità correlate sono caricate nell'esempio di memoria , ma applicate a qualsiasi query.
Si inizia estraendo (ma non eseguendo) la query radice aggregata ( GrandParents
nel proprio esempio) applicando tutto il necessario filtraggio:
var query = context.GrandParent.AsQueryable();
Quindi eseguire e materializzare la query principale, applicando include non filtrati:
var result = query
.Include(p => p.Parents)
.ToList();
Infine caricare esplicitamente i contenuti filtrati previsti:
query.SelectMany(p => p.Parents)
.SelectMany(c => c.Children)
.Where(t => t.Name != "NoInclude")
.Load();