-1

I need to pass an expression from a WinForms client to a WebApi. I have the following expression:

var results = somelist.Select(p => p.Id).ToList<int>();
Expression<Func<MyObj, bool>> myexp = x => results.Contains(x.Id);

I then simply did:

var str = myexp.Body.ToString();

However, in this particular example, the expression body looks like this:

value(MyApp.MyForm+<>c__DisplayClass41_0).results.Contains(x.Id)

Which obviously will not work when translating back the string to an expression server-side.

Is there a way to reduce, compile, whatever the expression in a better way? Or should I use 3rd party solutions like Remote.Linq or Serialize.Linq?

Ivan-Mark Debono
  • 15,500
  • 29
  • 132
  • 263
  • If you are using JSON.NET in both projects (WinForms and WebAPI), then you could try the solution described at the accepted answer of this question, https://stackoverflow.com/questions/23253399/serialize-expression-tree – Christos Oct 10 '20 at 16:15

1 Answers1

0

It depends on what you need to do with the expression on the server side.

If you really only need a string representation you might want to implement an ExpressionVisitor. However, this may require quite some effort, depending the kind of expressions you need to support.

In case you want to convert back to a proper expression tree and execute the expression on server side, it's definitely worth the have a look at Remote.Linq.

ChristofSenn
  • 337
  • 2
  • 6