problem with Linq Expressions

  • Thread starter Thread starter Lloyd Dupont
  • Start date Start date
L

Lloyd Dupont

I'm trying to create some linq expression manually.

At some stage I want to create an expression to concatenate 2 strings, I'm
trying the following code:

using System.Linq.Expressions;
Expression e1, e2;
return Expression.Call(typeof(string), "Concat", new Type[] {
typeof(string), typeof(string) }, e1, e2);

this call yield the following error:
System.InvaldOperationExpression
No method 'Concat' on type 'System.String' is compatible with the supplied
arguments.


Well, well, I'm somewhat surprised...
what else could I try?
 
Lloyd Dupont said:
I'm trying to create some linq expression manually.

At some stage I want to create an expression to concatenate 2 strings, I'm
trying the following code:

using System.Linq.Expressions;
Expression e1, e2;
return Expression.Call(typeof(string), "Concat", new Type[] {
typeof(string), typeof(string) }, e1, e2);

this call yield the following error:
System.InvaldOperationExpression
No method 'Concat' on type 'System.String' is compatible with the supplied
arguments.


Well, well, I'm somewhat surprised...
what else could I try?

Your new Type[] ... part is wrong. That means you're trying to call
string.Concat<string,string>(e1, e2).

Unfortunately, because there are multiple overloads of string.Concat
which take two parameters, you can't (AFAIK) use this form of
Expression.Call to create the expression. Instead, use

MethodInfo mi = typeof(string).GetMethod("Concat",
new [] {typeof(string), typeof(string) });
return Expression.Call(mi, e1, e2);
 
Thanks Jon!
Looks promising I will give it a try tonight!

As a side note I'm thinking to stop using expression, which are very
demanding (for example Expression.Add() fail when one expression has an Int
value and the other and has a Float value) and using the DLR for the same
purpose.

But Thanks to your tip I can go one step further... I will see...
 
Back
Top