I'm studying "Expression Tree" but I'm not managing to perform these expressions:
// first case
someList.Select(p => p.SomeProperty);
and
// second case
someList.Select(p => new OtherClass
{
SomeProperty = p.SomeProperty
})
To the "first case" I tried do this:
var someList = new List<SomeClass>();
someList.Add(new SomeClass { SomeProperty = "Hello" });
var someParam = Expression.Parameter(typeof(SomeClass), "p");
var someProperty = Expression.Property(someParam, "SomeProperty");
Expression.Call(
typeof(Enumerable),
"Select",
new Type[]
{
typeof(SomeClass),
typeof(string)
},
Expression.Lambda(
someProperty,
someParam
)
).Dump();
But I get this error:
InvalidOperationException: No generic method 'Select' on type 'System.Linq.Enumerable' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic.
About the "second case", I don't have ideia how to proceed.
Can anyone guide me here?
Calm down folks, after some research I found what was missing in my code...
On the fist case:
To the second case, I created the "new" expression through the code below:
Anyway, Thank you all for your help!