我正在尝试在LINQ中动态创建联接。 This answer让我对从哪里开始有了很好的了解。但是,当“foreing key”是int?
时,我特别遇到问题
(int
有效)。当它是int?
时,它将触发异常。
我已经制作了一个简单的LINQ to Objects示例来显示问题。为了运行我的示例,您需要install the dynamic linq Nuget package,因为我将其用于DynamicExpression.ParseLambda
方法。
这是触发异常的代码:
public class Contact
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Address
{
public int Id { get; set; }
public int? ContactId { get; set; }
public string Zip { get; set; }
}
class Program
{
static void Main(string[] args)
{
var contacts = new List<Contact>();
var addresses = new List<Address>();
LambdaExpression outerSelectorLambda = DynamicExpression.ParseLambda(typeof(Contact), null, "Id", new object[0]);
LambdaExpression innerSelectorLambda = DynamicExpression.ParseLambda(typeof(Address), null, "ContactId", new object[0]);
ParameterExpression[] parameters = new ParameterExpression[] { Expression.Parameter(typeof(Contact), "outer"), Expression.Parameter(typeof(Address), "inner") };
LambdaExpression resultsSelectorLambda = DynamicExpression.ParseLambda(parameters, null, "outer.Id", new object[0]);
// BLOWS UP HERE
var queryExpression = Expression.Call(
typeof(Queryable), "Join",
new Type[] { typeof(Contact), typeof(Address), typeof(int?), typeof(int) },
contacts.AsQueryable().Expression, addresses.AsQueryable().Expression,
Expression.Quote(outerSelectorLambda), Expression.Quote(innerSelectorLambda), Expression.Quote(resultsSelectorLambda));
// it will not reach the following line
var queryable = contacts.AsQueryable().Provider.CreateQuery(queryExpression);
}
}
要使上述代码生效,只需将int?
的{{1}}替换为int
。一切都会好起来的。问题在于Address.ContactId
是int?
。
问题是:为什么会发生这种情况?我该如何解决?。
异常信息(葡萄牙语):
System.InvalidOperationException was unhandled
HResult=-2146233079
Message=Nenhum método genérico 'Join' no tipo 'System.Linq.Queryable' é compatível com os argumentos e os argumentos de tipo fornecidos. Nenhum argumento de tipo deve ser fornecido se o método for não genérico.
Source=System.Core
StackTrace:
em System.Linq.Expressions.Expression.FindMethod(Type type, String methodName, Type[] typeArgs, Expression[] args, BindingFlags flags)
em System.Linq.Expressions.Expression.Call(Type type, String methodName, Type[] typeArguments, Expression[] arguments)
em problems.Program.Main(String[] args) na d:\POCs\problems\problems\Program.cs:linha 38
em System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
em System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
em Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
em System.Threading.ThreadHelper.ThreadStart_Context(Object state)
em System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
em System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
em System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
em System.Threading.ThreadHelper.ThreadStart()
InnerException:
答案 0 :(得分:3)
问题是您的密钥类型为int?
,但您用于创建密钥选择器的ParseLambda
方法是为您创建类型为Expression<Func<Contact, int>>
的表达式外键选择器。它没有将结果映射到可空的int。该函数与Join
方法的签名不兼容,因此抛出异常。
如果为了您的示例,您为选择器使用以下表达式,它将正常工作:
Expression<Func<Contact, int?>> outerSelectorLambda = c => c.Id;
查看ParseLambda
的API,您应该使用第二个参数来指定lambda的返回类型,所以不要将它留下null
:
DynamicExpression.ParseLambda(typeof(Contact), typeof(int?),
"Id", new object[0]);