-1

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;
    }
}
Zachary Scott
  • 20,968
  • 35
  • 123
  • 205
  • @Dr.Zim What do you get if you don't have the conversion? I'm not seeing why the conversion is necessary – Aaron Roberts Oct 16 '17 at 02:33
  • @AaronRoberts without the conversion, Entity Framework receives the job of converting the int to an object, which it does not understand. – Zachary Scott Oct 16 '17 at 02:47
  • @Backs I added the top edit to differentiate this question from the other, essentially it is not about extending a query, but storing the orderby parameter in a variable (so I can pass that parameter to a repository). – Zachary Scott Oct 16 '17 at 03:21
  • @Backs, @Tetsuya, @Yamamoto, @Servy This is not a duplicate. The other question asks how to `Entity.OrderBy("somefield")`. This question asks how to convert "somefield" in to a variable lambda, then use in the EntityFramework OrderBy() method. The OrderBy in this case is buried in a repository so I cannot add .OrderBy() to the Entity, plus I do not want to force the Entity in to an Enumerable then OrderBy, which is what happens when I convert the Object type to the literal type. – Zachary Scott Oct 23 '17 at 21:02
  • From review: I am not sure if the edit's and updates due to the "duplicate" mark made the question any clearer. Maybe it's best to try to ask the question again. – Stefan Oct 24 '17 at 22:55
  • @Stefan I tried to delete it but it will not let me. I don't know how to make it any clearer than an extension method is not a variable. – Zachary Scott Oct 25 '17 at 18:58
  • Ah, I know the drill. I have one of them floating around as well. Anyhow, voted to reopen, but I seldom see that happen. – Stefan Oct 25 '17 at 19:09

1 Answers1

-1

How about something like this?

    private static Dictionary<string, Func<Product,IComparable>> lookup= new 
  Dictionary<string, Func<Product,IComparable>>() { { "S", NewMethod}, { "D", NewMethodD}};


            private static IComparable NewMethod(Product a)
            {
                return a.SKU;
            }
            private static IComparable NewMethodD(Product a)
            {
                return  a.OtherTHing;
            }

you can then .OrderBy(lookup["S"]) or .OrderBy(lookup["D"])

Similar to what you had but associating it with something you can store (the key).

LoztInSpace
  • 5,584
  • 1
  • 15
  • 27
  • Essentially creating a huge case statement isn't going to work. `Expression.Lambda> (conversion, new[] { prm }).Compile();` turns my Entity's property name stored in a string in to the strongly typed Entity.Property where the code does not need to know about the Entity, it just matches the string to the property spelling. However, it's turning the Queryable into an Enumerable and doesn't send the orderby to SQL, which doesn't work. The answer is about fixing the translation code somehow. – Zachary Scott Oct 16 '17 at 12:19
  • Yeah fair enough. I agree this won't send the `order by` to the database. Good lesson that sometimes you have to just go old-school. I suspect a view might be the best of a bad set of options here. It's just much easier to understand. – LoztInSpace Oct 16 '17 at 12:52