I have extension methods that I am using in the query. Here are the methods:
public static class UOMExtensions
{
public static UOM GetSourceUOM(this UOM uOM)
{
//Calculate something
return uOM;
}
public static UOM GetParentUOM(this UOM uOM)
{
var sourceUOM = uOM.GetSourceUOM();
//Do something recoursively
return sourceUOM;
}
public static bool IsCorrectUOM(this UOM uOM)
{
var parent = uOM.GetParentUOM();
//Do something
return true;
}
}
Here is my query:
using (Context context = new Context())
{
var uoms = context.Set<UOM>().Where(a => a.IsCorrectUOM()).ToList();
}
I got this exception:
System.NotSupportedException: 'LINQ to Entities does not recognize the method 'Boolean >IsCorrectUOM(ConsoleApp1.UOM)' method, and this method cannot be translated into a store expression.'
After that, I tried to create an expression for that using NJection.LambdaConverter
:
public static bool IsCorrectUOMMethod(UOM uOM)
{
var parent = uOM.GetParentUOM();
//Do something
return true;
}
public static Expression<Func<UOM, bool>> IsCorrectUOMExression()
{
var lambda = Lambda.TransformMethodTo<Func<UOM, bool>>()
.From(() => IsCorrectUOMMethod)
.ToLambda();
return lambda;
}
My query became to:
using (Context context = new Context())
{
var uoms = context.Set<UOM>().Where(UOMExtensions.IsCorrectUOMExression()).ToList();
}
After that I got this exception:
System.NotSupportedException: 'Unknown LINQ expression of type 'Block'.'
How can I execute my query successfully? I cannot change or remove my methods because they do complex things and have a lot of usages.