2016-07-10 6 views
0

zur Verfügung gestellt wurde habe ich den folgenden Arbeits Code:Die Art oder Methode hat drei allgemeine Parameter (s), aber zwei generisches Argument (e)

Context context = new Context(_options); 

Expression<Func<Context.Person, Context.Address>> e1 = x => x.Address; 
Expression<Func<Context.Address, Context.Country>> e2 = x => x.Country; 

IIncludableQueryable<Context.Person, Context.Address> a = context.Persons.Include(e1); 
IIncludableQueryable<Context.Person, Context.Country> b = a.ThenInclude(e2); 

List<Context.Person> result = context.Persons.Include(e1).ThenInclude(e2).ToList(); 

Wo Include und ThenInclude sind Methoden Erweiterung Entity Framework-Core.

In meinem Code muss ich generische Typen verwenden, so dass ich Reflexion bin mit:

IQueryable<Context.Person> persons = context.Persons; 

MethodInfo include = typeof(EntityFrameworkQueryableExtensions).GetMethods().First(x => x.Name == "Include" && x.GetParameters().Select(y => y.ParameterType.GetGenericTypeDefinition()).SequenceEqual(new[] { typeof(IQueryable<>), typeof(Expression<>) })); 

MethodInfo thenInclude = typeof(EntityFrameworkQueryableExtensions).GetMethods().First(x => x.Name == "ThenInclude" && x.GetParameters().Select(y => y.ParameterType.GetGenericTypeDefinition()).SequenceEqual(new[] { typeof(IIncludableQueryable<,>), typeof(Expression<>) })); 

Expression<Func<Context.Person, Context.Address>> l1 = x => x.Address; 

Expression<Func<Context.Address, Context.Country>> l2 = x => x.Country; 


try { 

    MethodInfo includeInfo = include.MakeGenericMethod(typeof(Context.Person), l1.ReturnType); 

    IIncludableQueryable<Context.Person, Context.Address> r1 = (IIncludableQueryable<Context.Person, Context.Address>)includeInfo.Invoke(null, new Object[] { persons, l1 }); 

    MethodInfo thenIncludeInfo = thenInclude.MakeGenericMethod(typeof(Context.Address), l2.ReturnType); 

    IIncludableQueryable<Context.Address, Context.Country> r2 = (IIncludableQueryable<Context.Address, Context.Country>)thenIncludeInfo.Invoke(null, new Object[] { r1, l2 }); 

    var r = r2.AsQueryable(); 

} catch (Exception ex) { } 

Aber auf dieser Code-Zeile:

MethodInfo thenIncludeInfo = thenInclude.MakeGenericMethod(typeof(Context.Address), l2.ReturnType); 

bekomme ich folgende Fehlermeldung:

The type or method has 3 generic parameter(s), but 2 generic argument(s) were provided. A generic argument must be provided for each generic parameter. 

Ich kann den Fehler verstehen, indem ich auf ThenInclude Definition schaue, aber ich bin mir nicht sicher, wie man es löst ...

+1

Die Signatur von 'ThenInclude' erwartet 3 generischen Parameter (' '), aber sie vorbei sind nur 2. – haim770

+0

@ haim770 I versuchte, 3 mit "MethodInfo thenIncludeInfo = thenInclude.MakeGenericMethod (typeof (Kontext.Person), typeof (Kontext.Adresse), l2.ReturnType);" aber dann bekomme ich den Fehler "Objekt des Typs 'Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions + IncludableQuer yable2 [Person, Address]' kann nicht in den Typ 'Microsoft.EntityFrameworkCore.Query.IIncludableQueryable2 [Person, System.Collections.Generic. ICollection'1 [Adresse]] '. ". Weißt du, warum? –

Antwort

3

Wie die Nachricht andeutet, erwartet ThenInclude 3 Typparameter: TEntity, TPreviousProperty, TProperty. Aus dem Code, so scheint es, dies funktionieren würde:

MethodInfo thenIncludeInfo = thenInclude.MakeGenericMethod(
    typeof(Context.Person), typeof(Context.Address), l2.ReturnType); 
+1

Das erste generische Argument ist 'TEntity', das (so weit ich aus dem OP-Code folgern kann)' Person' ist. Das zweite Argument ist 'TPreviousProperty', das ist der Typ der Eigenschaft, die an das erste' Include() '(in diesem Fall' Address') übergeben wird. – haim770

+0

Ich habe Ihren Vorschlag versucht, aber jetzt bekomme ich den Fehler "Objekt des Typs 'Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions + IncludableQueryable'2 [Person, Adresse]' kann nicht in den Typ 'Microsoft.EntityFrameworkCore.Query.IIcludableQueryable'2 [Person, System .Collections.Generic.ICollection'1 [Adresse]] '. " ... Nicht sicher warum ... Weißt du es? –

+0

@MiguelMoura Hast du es funktioniert? Es funktioniert gut für mich. –

Verwandte Themen