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();
This is the error:
Unrecognised method call in epression x.GetDataUniverseId()
Background:
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
.
The method GetAvailableIdsPerDataUniverse(x => x.GetDataUniverseId())
has the following signature:
protected DetachedCriteria
GetAvailableIdsPerDataUniverse(System.Linq.Expressions.Expression<Func<Fund, object>> dataUniverseId)
and x => x.GetDataUniverseId()
is defined in my Fund
class as
public virtual System.Linq.Expressions.Expression<Func<Fund, object>> GetDataUniverseId()
{
return f => f.Id;
}
and for example as the following in the Company
class:
public virtual Expression<Func<Fund, object>> GetDataUniverseId()
{
return f => f.Company.Id;
}
This works if instead 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.
Question:
Is there anything I can do to resolve this error?
Note:
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 paramater that I 'hard-coded' it worked fine.
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())))