Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to turn a string into a linq expression?

Similar: Convert a string to Linq.Expressions or use a string as Selector?

A similar one of that one: Passing a Linq expression as a string?

Another question with the same answer: How to create dynamic lambda based Linq expression from a string in C#?

Reason for asking something which has so many similar questions:

The accepted answer in those similar questions is unacceptable in that they all reference a library from 4 years ago (granted that it was written by code master Scott Gu) written for an old framework (.net 3.5) , and does not provide anything but a link as an answer.

There is a way to do this in code without including a whole library.

Here is some sample code for this situation:

    public static void getDynamic<T>(int startingId) where T : class
    {
        string classType = typeof(T).ToString();
        string classTypeId = classType + "Id";
        using (var repo = new Repository<T>())
        {
            Build<T>(
             repo.getList(),
             b => b.classTypeId //doesn't compile, this is the heart of the issue
               //How can a string be used in this fashion to access a property in b?
            )
        }
    }

    public void Build<T>(
        List<T> items, 
        Func<T, int> value) where T : class
    {
        var Values = new List<Item>();
        Values = items.Select(f => new Item()
        {
            Id = value(f)
        }).ToList();
    }

    public class Item
    {
     public int Id { get; set; }
    }

Note that this is not looking to turn an entire string into an expression such as

query = "x => x.id == somevalue";

But instead is trying to only use the string as the access

query = x => x.STRING;
like image 926
Travis J Avatar asked Jul 13 '12 23:07

Travis J


2 Answers

Here's an expression tree attempt. I still don't know if this would work with Entity framework, but I figure it is worth a try.

Func<T, int> MakeGetter<T>(string propertyName)
{
    ParameterExpression input = Expression.Parameter(typeof(T));

    var expr = Expression.Property(input, typeof(T).GetProperty(propertyName));

    return Expression.Lambda<Func<T, int>>(expr, input).Compile();  
}

Call it like this:

Build<T>(repo.getList(), MakeGetter<T>(classTypeId))

If you can use an Expression<Func<T,int>> in place of a just a Func, then just remove the call to Compile (and change the signature of MakeGetter).


Edit: In the comments, TravisJ asked how he could use it like this: w => "text" + w.classTypeId

There's several ways to do this, but for readability I would recommend introducing a local variable first, like this:

var getId = MakeGetter<T>(classTypeId);

return w => "text" + getId(w); 

The main point is that the getter is just a function, and you can use it exactly like you normally would. Read Func<T,int> like this: int DoSomething(T instance)

like image 100
Paul Phillips Avatar answered Oct 07 '22 08:10

Paul Phillips


Here is an extension method for you with my testing code (linqPad):

class test
{
   public string sam { get; set; }
   public string notsam {get; set; }
}

void Main()
{
   var z = new test { sam = "sam", notsam = "alex" };

   z.Dump();

   z.GetPropertyByString("notsam").Dump();

   z.SetPropertyByString("sam","john");

   z.Dump();
}

static class Nice
{
  public static void  SetPropertyByString(this object x, string p,object value) 
  {
     x.GetType().GetProperty(p).SetValue(x,value,null);
  }

  public static object GetPropertyByString(this object x,string p)
  {
     return x.GetType().GetProperty(p).GetValue(x,null);
  }
}

results:

results

like image 2
Hogan Avatar answered Oct 07 '22 08:10

Hogan