How to get value from string expression

c# dynamic-linq linq linq-expressions reflection

Question

I have a string stored in my db:

"Users.ElementAt(1).LastName"

I then have an object like so:

        MyClass myclass = new MyClass ()
        {
            Users = new List<User>()
            {
                new User()
                {
                    LastName = "LastName1"
                },
                new User()
                {
                    LastName = "LastName2"
                },
            }
        };  

Is there a way to either parse/evaluate/run the given string against my object to get each users last name?

I've been using the DynamicLinq library, but I am running into an issue with ElementAt(1) with the error message:

"No applicable aggregate method 'ElementAt' exists"

Can anyone provide some guidance here? Will I have to resort to writing my own parser and using reflection?

1
0
9/27/2013 2:26:15 AM

Accepted Answer

It looks like DynamicLinq's expression parser only supports a subset of the standard LINQ operator methods:

interface IEnumerableSignatures
{
    void Where(bool predicate);
    void Any();
    void Any(bool predicate);
    void All(bool predicate);
    void Count();
    void Count(bool predicate);
    void Min(object selector);
    void Max(object selector);
    void Sum(int selector);
    void Sum(int? selector);
    void Sum(long selector);
    void Sum(long? selector);
    void Sum(float selector);
    void Sum(float? selector);
    void Sum(double selector);
    void Sum(double? selector);
    void Sum(decimal selector);
    void Sum(decimal? selector);
    void Average(int selector);
    void Average(int? selector);
    void Average(long selector);
    void Average(long? selector);
    void Average(float selector);
    void Average(float? selector);
    void Average(double selector);
    void Average(double? selector);
    void Average(decimal selector);
    void Average(decimal? selector);
    void Take(int count);
    void Union(IQueryable right);
    void OrderBy(LambdaExpression exp);
    void OrderByDescending(LambdaExpression exp);
}

As you can see, ElementAt is not among them. The good news is that simply adding void ElementAt(int index); to IEnumerableSignatures appears to work just fine. That is, of course, assuming the LINQ query provider you are using will handle the ElementAt operator; it is entirely possible that it will not.

But if modifying the DynamicLinq source isn't an option, then no, there are no facilities within the core .NET Framework or LINQ which will parse and evaluate those kinds of expressions. There are other options (see ScriptCS, Roslyn), but I suspect you are drifting towards an "overkill" solution to whatever actual problem you are trying to solve.

0
10/10/2013 4:34:40 PM

Popular Answer

var allLastNamesInList= Users.Select(x=> x.LastName).toList();

This statement will iterate through the list and get all last names. You can even use order by as well so that you will get last names in order.

Is this is the requirement you are looking for or something different ?



Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow