How to get value from string expression

c# dynamic-linq linq linq-expressions reflection


I have this string in my database:


I then have the following item:

        MyClass myclass = new MyClass ()
            Users = new List<User>()
                new User()
                    LastName = "LastName1"
                new User()
                    LastName = "LastName2"

Is there a method to parse, analyze, or execute the supplied text against my object in order to get the last name of each user?

I've been using the DynamicLinq library, however I'm having trouble using ElementAt(1) and getting the following error:

There is no aggregate method 'ElementAt' that applies.

Can somebody provide some advice in this situation? Will I be forced to use reflection and write my own parser?

9/27/2013 2:26:15 AM

Accepted Answer

It seems that just a portion of the common LINQ operator methods are supported by DynamicLinq's expression parser:

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);

You can see thatElementAt not one of them. The good news is that just includingvoid ElementAt(int index); to IEnumerableSignatures seems to operate without issue. Naturally, it assumes the LINQ query provider you're using can handle theElementAt operator; but, there is a chance that it won't.

There are no tools inside the core.NET Framework or LINQ that will read and evaluate such types of expressions, however, if changing the DynamicLinq source isn't a possibility. Although there are alternative possibilities (see ScriptCS, Roslyn), I have a feeling you are headed towards a "overkill" approach to the real issue you are attempting to address.

10/10/2013 4:34:40 PM

Popular Answer

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

This command will loop over the list and get every last name. To retrieve last names in order, you may also utilize the order by option.

Is this the criteria you're searching for, or are you seeking for something else?

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