如何获取返回表达式的方法来解决“无法识别的方法调用"错误? [英] How to get a method to return an expression to resolve 'Unrecognised method call in epression' error?

查看:92
本文介绍了如何获取返回表达式的方法来解决“无法识别的方法调用"错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下陈述引发了我不理解的错误:

I have the following statement that's throwing an error I don't understand:

return (int) _session.CreateCriteria<T>()
    .Add(LambdaSubquery.Property<Fund>(x => x.Id)
        .In(GetAvailableIdsPerDataUniverse(x => x.GetDataUniverseId())))
    .AddNameSearchCriteria<T>(searchExpression)
    .SetProjection(LambdaProjection.Count<T>(e => e.Id))
    .UniqueResult();

这是错误:

x.GetDataUniverseId()中的无法识别的方法调用

Unrecognised method call in epression x.GetDataUniverseId()

背景:

我需要执行搜索的一些不同类型.这些类型中的每一个都实现ISearchable,这要求它们具有Name& Id属性以及GetDataUniverseId()方法,该方法将返回一个表达式,该表达式表示LambdaProjection所需的dataUniverseId.

I have some different types that I need to perform searches for. Each of these types implement ISearchable which requires that they have Name & Id properties as well as the GetDataUniverseId() method, which will return an expression representing the dataUniverseId needed for the LambdaProjection.

方法GetAvailableIdsPerDataUniverse(x => x.GetDataUniverseId())具有以下签名:

protected DetachedCriteria 
GetAvailableIdsPerDataUniverse(System.Linq.Expressions.Expression<Func<Fund, object>> dataUniverseId)

x => x.GetDataUniverseId()在我的Fund类中定义为

public virtual System.Linq.Expressions.Expression<Func<Fund, object>> GetDataUniverseId()
{
    return f => f.Id;
}

,例如,在Company类中如下所示:

and for example as the following in the Company class:

public virtual Expression<Func<Fund, object>> GetDataUniverseId()
{
    return f => f.Company.Id;
}

如果不是硬编码" f => f.Idf => f.Company.Id而不是尝试通过x => x.GetDataUniverseId()作为参数,则此方法有效.如果要处理的类型能够提供所需的表达式,而不是必须将该表达式传递给使用GetAvailableIdsPerDataUniverse()的每个方法,我都希望这样做.我以为,可以访问返回表达式的方法,而不是访问推断类型的属性.

This works if instead of trying to pass x => x.GetDataUniverseId() as the parameter, I 'hard-code' f => f.Id or f => f.Company.Id. I'd like it if the type being worked on was able to supply the expression it needed rather than me having to pass that expression into every method that uses GetAvailableIdsPerDataUniverse(). I thought that instead of accessing a property on the inferred type, it could execute a method that would return the expression.

问题:

我有什么办法可以解决此错误?

Is there anything I can do to resolve this error?

注意:

一切都正常,因此运行它时抛出错误时,我感到有些惊讶.主要是因为当我将表达式作为我硬编码"的参数传递时,它可以正常工作.

Everything builds fine, so I was a little surprised to see that it threw an error when I ran it. Mostly because when I passed in the expression as a parameter that I 'hard-coded' it worked fine.

此外,我也尝试了以下操作,但都没有成功,我在其中指定了我要对GetAvailableIdsPerDataUniverse进行操作的类型:

Also, I've also tried the following to no avail, where I specify the type I want GetAvailableIdsPerDataUniverse to act on:

.In(GetAvailableIdsPerDataUniverse<Fund>(x => x.GetDataUniverseId())))

推荐答案

我被告知这里的问题是我想要多态静态方法的行为,但是我不能这样做,因为GetDataUniverseId不是实例方法,并且我需要一个实例来能够多态使用它.

I've been informed that the problem here is that I want the behaviour of polymorphic static methods, but I can't have that because GetDataUniverseId is not an instance method, and I need an instance to be able to use it polymorphically.

该解决方案虽然效率低,因为它使用了大量反射,但仍使用new T()

The solution, although inefficient because it uses lots of reflection is to use new T()

如此

    .Add(LambdaSubquery.Property<Fund>(x => x.Id)
        .In(GetAvailableIdsPerDataUniverse(x => x.GetDataUniverseId())))

成为

    .Add(LambdaSubquery.Property<Fund>(x => x.Id)
        .In(GetAvailableIdsPerDataUniverse((new T()).GetDataUniverseId())))

与此有关的唯一另一个问题是该方法存在于内部:

The only other issue with this is that the method this exists inside:

    public IEnumerable<T> GetEntitiesByName<T>(int pageSize, string searchExpression)
        where T : class, ISearchableEntity

现在必须具有new()约束,如下所示:

must now have the new() constraint, as follows:

    public IEnumerable<T> GetEntitiesByName<T>(int pageSize, string searchExpression) 
        where T : class, ISearchableEntity, new()

我希望有人能从@Sunny提供的答案"中找到更多的用途

I hope somebody finds more use from this than the 'answer' provided by @Sunny

这篇关于如何获取返回表达式的方法来解决“无法识别的方法调用"错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆