0

Is it possible to do what I'm looking for here:

namespace ExpressionProblem
{
    class Program
    {
        private static readonly List<dynamic> Items = new List<dynamic>
        {
            new { Name = "Foo" },
            new { Name = "Bar" }
        };

        static void Main()
        {
            var result = DoSomething<Item>(p => p.Name == "Foo");

            Console.WriteLine(result.Name);
        }

        static T DoSomething<T>(Expression<Func<T, bool>> expression)
        {
            //change expression lambda from Func<T, bool> to Func<dynamic, bool> so below will compile and work

            return Items.FirstOrDefault(expression);
        }
    }

    public class Item
    {
        public string Name { get; set; }
    }
}

Basically I need to take the given expression with has a signature of

Expression<Func<T, bool>> 

and get and expression which performs the same action but has the signature

Expression<Func<dynamic, bool>>.  

Is this even possible? From what I've read I don't think you can change an existing expression as most places I've researched have said you basically have to build a new expression from an existing one. I tried to do the following:

var newExpression = Expression.Lambda<Func<dynamic, bool>>(expression.Body);

...but am getting the error "Incorrect number of parameters supplied for lambda declaration" when trying to create the new expression.

Any ideas on how to get this to work or am I trying to do something that can't (or shouldn't) be done?

Thanks in advance for any help you can provide.

Regards,

Craig

[EDIT] - I know some will ask why I don't just make the list of type Item - in my particular case I am not able to as the list to be queried will be in an app domain that has no knowledge of the Item type. I'm just including it in the same namespace/class here for brevity's sake.[/EDIT]

Craig Koster
  • 496
  • 5
  • 15
  • possible duplicate of [c# Cast List to List](http://stackoverflow.com/questions/5115275/c-sharp-cast-listx-to-listy) – Hogan Dec 12 '14 at 16:42
  • @CK1 in regards to your Edit comment, anything working on the result of that function implicitly has knowledge of the type, or at least aspects of the type. You can use a common interface or parent type w/ a reduced set of fields to share information between the two domains. – Chris Dec 12 '14 at 16:42
  • you can use `List.ConvertAll([Converter from Y to T]);` – Hogan Dec 12 '14 at 16:43
  • I can't cast the list to anything as I don't have the type information to cast it to. See edit above. – Craig Koster Dec 12 '14 at 16:47
  • If I understand the question correctly (and I may not), what you want is impossible. Expression trees are prohibited from containing dynamic operations. If you were trying to convert to any type other than `dynamic`, rewriting the `Expression` programmatically could work. But not with `dynamic`. – Peter Duniho Dec 12 '14 at 17:49
  • Right, so you must require the function `Func()` to exist -- then it is possible. – Hogan Dec 12 '14 at 18:55
  • Why are you doing `new List` instead of `new List`? The list you create with `dynamic` does not have `Item` instances inside it. It has an anonymous type that happen to have one property, `Name`. These only superficially resemble `Item` in having the same property list--they are in fact different objects. There's no way to easily and generically convert from a dynamic object to a strongly-typed object. That's a much more complicated process of mapping one type of object to another at runtime. What if your input anonymous type object has an additional property not in `Item`? – ErikE Oct 30 '15 at 20:03

0 Answers0