3
我選擇在LINQ Join查詢中爲臨時投影創建一個匿名類型。我正在使用ExpressionTrees在運行時構建查詢。我不知道下面的代碼是否可以幫助我創建一個臨時的投影序列。確定動態類型的返回類型
這裏是做臨時投影代碼:
private Expression<Func<EntityObject, EntityObject,dynamic>> TempProjectionExpression
{
get
{
return (o, p) => new
{
o = o,
p = p
};
}
}
我使用表達式樹連接查詢下面給出。
public IQueryable<dynamic> GetQueryExpressionresults3<T, U, V>(IQueryable<T> aEntityCollection1, IQueryable<U> aEntityCollection2, Type[] _TypeArguments ,V _anonymousType, string aPropertyName)
where T : EntityObject
where U : EntityObject
{
ParameterExpression pe = Expression.Parameter(typeof(U), "o");
ParameterExpression pe1 = Expression.Parameter(typeof(T), "p");
//This should be populated from UI
Expression me = Expression.Property(pe1, typeof(T).GetProperty(aPropertyName));
//This should be populated from UI
Expression me1 = Expression.Property(pe, typeof(U).GetProperty(aPropertyName));
LambdaExpression le = Expression.Lambda<Func<T, int>>(me, new ParameterExpression[] { pe1 });
LambdaExpression le1 = Expression.Lambda<Func<U, int>>(me1, new ParameterExpression[] { pe });
_TypeArguments = new Type[] { aEntityCollection1 .ElementType, aEntityCollection2.ElementType, le.Body.Type, typeof(MovieCollections)};
//_TypeArguments = _TypeArguments.Concat(new Type[] { le.Body.Type, typeof(object) }).ToArray();
MethodCallExpression JoinCallExpression = Expression.Call(typeof(Queryable), "Join", _TypeArguments, aEntityCollection1.Expression, aEntityCollection2.Expression
, le, le1, TempProjectionExpression);
var oResult = aEntityCollection1.Provider.CreateQuery(JoinCallExpression) as IQueryable<dynamic>;
return oResult;
}
現在的問題是,我想確定TempProjectionExpression的返回類型,即typeof(動態)。這可能嗎?如果是,那麼如何?
typeof(dynamic)給出編譯時錯誤。我已經嘗試使用TempProjectionExpression.Body.Type來確定動態類型的返回類型。它不會給出任何編譯器錯誤,但是我不知道它是否正確。 –