2

我正在尝试将其转换为:

ISession.Query<Question>()
    .Where(x => x.QuestionId == q.QuestionId)
    .FetchMany(x => x.Answers)
    .ToFuture();

反射类型:

IQueryable<Question> query = ISession.Query<Question>().Where(string.Format("{0} = @0", "QuestionId"), q.QuestionId);
Type emType = typeof(NHibernate.Linq.EagerFetchingExtensionMethods);
MethodInfo mi = emType.GetMethod("FetchMany");

ParameterExpression paramEx = Expression.Parameter(typeof(Question), "x");
MemberExpression me = Expression.Property(paramEx, "Answers");         
LambdaExpression lambdaEx = Expression.Lambda (me, paramEx);            

// runtime error here
mi.MakeGenericMethod(typeof(Question), typeof(Answer)).Invoke(emType, new object[] { query, lambdaEx }); 

运行时错误:

System.ArgumentException: Object of type 
'System.Linq.Expressions.Expression`1
[System.Func`2[TestXXX.TheModels.Question,
System.Collections.Generic.IList`1[TestXXX.TheModels.Answer]]]' 

cannot be converted to type

'System.Linq.Expressions.Expression`1
[System.Func`2[TestXXX.TheModels.Question,
System.Collections.Generic.IEnumerable`1[TestXXX.TheModels.Answer]]]'.

POCO 样本:

问题的答案是 IList,请不要建议将 IList 更改为 IEnumerable ;-)

public class Question
{
    public virtual int QuestionId { get; set; }
    public virtual string QuestionText { get; set; }

    public virtual IList<Answer> Answers { get; set; }
}

这是 FetchMany 的方法签名:

namespace NHibernate.Linq
{
    public static class EagerFetchingExtensionMethods
    {
        public static INhFetchRequest<TOriginating, TRelated> Fetch<TOriginating, TRelated>(this IQueryable<TOriginating> query, Expression<Func<TOriginating, TRelated>> relatedObjectSelector);
        public static INhFetchRequest<TOriginating, TRelated> FetchMany<TOriginating, TRelated>(this IQueryable<TOriginating> query, Expression<Func<TOriginating, IEnumerable<TRelated>>> relatedObjectSelector);
        public static INhFetchRequest<TQueried, TRelated> ThenFetch<TQueried, TFetch, TRelated>(this INhFetchRequest<TQueried, TFetch> query, Expression<Func<TFetch, TRelated>> relatedObjectSelector);
        public static INhFetchRequest<TQueried, TRelated> ThenFetchMany<TQueried, TFetch, TRelated>(this INhFetchRequest<TQueried, TFetch> query, Expression<Func<TFetch, IEnumerable<TRelated>>> relatedObjectSelector);
    }
}

应该怎么做才能在反射期间将 IList 传递给 NHibernate 的 FetchMany 的 IEnumerable?

注意:如果我们不使用反射,可以将 IList 传递给 IEnumerable

4

1 回答 1

2

改变这个

MemberExpression me = Expression.Property(paramEx, "Answers");         

对此:

Expression me = Expression.Convert(Expression.Property(paramEx, "Answers"), typeof(IEnumerable<Answer>));

您收到错误的原因是您的 lambda 的委托类型不同意。IList<T>我们需要通过人为地将 a视为来使类型兼容IEnumerable<T>

正如任何动态语言的支持者都会说的那样,这是一种 hack ;-) 无论如何,它可靠地工作。

于 2012-06-19T16:11:19.077 回答