[英]The type or method has 3 generic parameter(s), but 2 generic argument(s) were provided
I have the following working code: 我有以下工作代码:
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();
Where Include and ThenInclude are Entity Framework Core extension methods. 其中Include和ThenInclude是Entity Framework Core扩展方法。
In my code I will need to use generic types so I am using reflection: 在我的代码中,我将需要使用泛型类型,因此我在使用反射:
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) { }
But on this code line: 但是在此代码行上:
MethodInfo thenIncludeInfo = thenInclude.MakeGenericMethod(typeof(Context.Address), l2.ReturnType);
I get the following error: 我收到以下错误:
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.
I can understand the error by looking at ThenInclude definition but I am not sure how to solve it ... 我可以通过查看ThenInclude定义来理解该错误,但不确定如何解决...
As the message suggests, ThenInclude
expects 3 type parameters: TEntity, TPreviousProperty, TProperty
. 如消息所提示,
ThenInclude
需要3个类型参数: TEntity, TPreviousProperty, TProperty
。 From your code, it would seem this would work: 从您的代码来看,这似乎可行:
MethodInfo thenIncludeInfo = thenInclude.MakeGenericMethod(
typeof(Context.Person), typeof(Context.Address), l2.ReturnType);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.