I have given up trying to explain the difference between these two questions and have tried to delete this question but the system won't let me. I am sorry I wasted everyone's time.
But there definitely is a difference between these two things that each question addresses separately:
Entity.OrderBy("field name") //<-- Extension method
and
Expression<Func<TE,TK>> myvar;
Entity.OrderBy( myvar) //<-- Variable holding an Expression
I am trying to create a function to convert a string name in to a Lambda Expression that refers to an Entity property without using an Extension Method. The repository we use accepts Lambdas so we cannot send it Extension Methods.
// LinqPad for testing
void Main()
{
string prop = "sku";
// We can store data type of property in variable, but how
// do we convey that value in the <> brackets?
Expression<Func<[Entity type],[Property type]>> orderby;
orderby = Me.GetOrder<Product,[How do I get SKU type here?]>("sku");
Product.OrderBy( orderby).Skip(10).Take(10).Dump();
}
public static class Me
{
public static Expression<Func<TE, TK>> GetOrder<TE,TK>(
string propertyName) where TE: class
{
//Create x=>x.PropName
var propertyInfo = typeof(TE).GetProperty(propertyName);
ParameterExpression arg = Expression.Parameter(typeof(TE), "x");
MemberExpression property = Expression.Property(arg, propertyName);
var selector = Expression.Lambda<Func<TE,Object>>(property,
new ParameterExpression[] { arg });
return selector;
}
}