0

Before I had

public static System.Linq.Expressions.Expression<Func<MetaMovie, bool>> IsEqual(MetaMovie other)
{
    if (other.Id > 0) return m => other.Id == m.Id;

    return m => other.MetaSource == m.MetaSource && other.ExternalId == m.ExternalId;
}

DebugView of a variable assigned the call of the above method:

.Lambda #Lambda1<System.Func`2[com.cyberinternauts.all.MediaRecognizer.Models.Metas.MetaMovie,System.Boolean]>(com.cyberinternauts.all.MediaRecognizer.Models.Metas.MetaMovie $m)
{
    (.Constant<com.cyberinternauts.all.MediaRecognizer.Models.Metas.MetaMovie+<>c__DisplayClass81_0>(com.cyberinternauts.all.MediaRecognizer.Models.Metas.MetaMovie+<>c__DisplayClass81_0).other).MetaSource ==
    $m.MetaSource && (.Constant<com.cyberinternauts.all.MediaRecognizer.Models.Metas.MetaMovie+<>c__DisplayClass81_0>(com.cyberinternauts.all.MediaRecognizer.Models.Metas.MetaMovie+<>c__DisplayClass81_0).other).ExternalId ==
    $m.ExternalId
}

... that I would like to obtain the same using...

Now I want

I would like to create a method Func<MetaMovie, bool> NAME(MetaMovie other) that would be called by IsEqual. I did the new method called GetComparison, but I am not able to integrate it my IsEqual method.

I tried

public static Func<MetaMovie, bool> GetComparison(MetaMovie other)
{
    if (other.Id > 0) return m => other.Id == m.Id;

    return m => other.MetaSource == m.MetaSource && other.ExternalId == m.ExternalId;
}

public static Expression<Func<MetaMovie, bool>> IsEqual(MetaMovie other)
{
    var param1 = Expression.Parameter(other.GetType(), "param1");

    return Expression.Lambda<Func<MetaMovie, bool>>(Expression.Call(GetComparison(other).Method), param1);
}

Error

I am getting the follow error on the return line of IsEqual Static method requires null instance, non-static method requires non-null instance

Master DJon
  • 1,899
  • 2
  • 19
  • 30
  • Consider what your `return` statement is doing: you are creating a lambda that looks like `param1 => ((Func<>)GetComparison(other))()` - where is the parameter `m` being passed into the return from `GetComparison`? – NetMage Feb 21 '23 at 16:13
  • @NetMage You are right. The question is, how do I pass it? – Master DJon Feb 21 '23 at 20:41
  • You must use `Expression.Invoke(Expression.Constant(GetComparison(other)), param1)` to invoke the lambda. See [this answer](https://stackoverflow.com/a/30917040/2557128). – NetMage Feb 21 '23 at 22:24
  • Note: Since your delegate is an instance method (of a compiler created class to store the value of `other` required since your lambda isn't `static`) you could also use: `var cmpFunc = GetComparison(other); var compareCall = Expression.Call(Expression.Constant(cmpFunc.Target), cmpFunc.Method, param1); return Expression.Lambda>(compareCall, param1);` – NetMage Feb 21 '23 at 22:42
  • @NetMage Unfortunately, none of those works. I want the debug view to be identical. I'll explain why: I have a class that needs to verify two like this one (including this one). The goal is to use this in EntityFrameworkCore Where. https://stackoverflow.com/questions/75394473/c-sharp-combine-expressions-from-two-others-using-a-different-property-for-each – Master DJon Feb 22 '23 at 03:13
  • @NetMage Shall I delete this question? – Master DJon Feb 22 '23 at 19:30

0 Answers0