1

I am new to the System.Linq.Expressions namespace, but it seems you can do some pretty awesome things.

I would like to create a "self projecting" lambda expression using the System.Linq.Expressions objects. Something like:

 list.Select(element => element);

I stumbled across a sample where they build a "property projecting" lambda expression using the following snippet:

 //creates something like: list.Select(element => element.[propertyName])

 var parameter = Expression.Parameter(elementType, "posting");
 Expression property = Expression.Property(parameter, propertyName);

 LambdaExpression lambda = Expression.Lambda(property, new[] { parameter });

But how I can create a self-projecting lambda?

Thanks!

John Russell
  • 2,177
  • 4
  • 26
  • 47
  • 1
    The recent http://stackoverflow.com/questions/1466689/linq-identity-function may be of interest – AakashM Aug 24 '11 at 15:01
  • Why would you want to do that? `list` is probably `IEnumerable` and so will the result of `list.Select(e => e)` also be, right? – Mikael Östberg Aug 24 '11 at 15:02
  • I'm trying to do this without knowing T until run time. I'm using the System.Linq.Expressions objects and reflection to invoke OrderBy dynamically. – John Russell Aug 24 '11 at 15:09
  • FYI, a "self-projecting" lambda is usually called an "identity" lambda, because its output is "identical" to its input. – Eric Lippert Aug 24 '11 at 16:01

1 Answers1

3

Would the following do what you require:

var parameter = Expression.Parameter(elementType, "posting");
var lambda = Expression.Lambda(parameter, new[] { parameter });
Iridium
  • 23,323
  • 6
  • 52
  • 74
  • Thanks. That makes sense. Using the example above (list.Select(element => element)), it looks like "parameter" represents "element". – John Russell Aug 24 '11 at 15:13