Transform Expression<IBase> to Expression<User> where User implements IBase RRS feed

  • Question

  • Hi all,

    I'm trying to formulate a general Expression against IBase interface, for example, that the ID should be not equal to null or ""; afterwards, I want this expression to be combined with an Expression against a User. I found some pretty nice solutions for combining expressions out there, but none of them supports combination of Expressions that are against different types, which (although I have to admin I got limited understanding of this LinQ and Expression stuff) from my point of view should not be a problem, because everything that can be expressed for a IBase, should be express-able for a User as well, if it implements the IBase interface, isn't it?

    Look forward to you replies,

    Tuesday, June 3, 2008 9:18 AM

All replies

  • Hi Simon,

    plese give a pseude-code example of what you are trying to do. What I understood is that you are trying to do something like:

    Func<User, bool> f = user => !string.IsNullOrEmpty(user.ID) && !string.IsNullOrEmpty(user.Name); // IBase does not contain property Name, User does
    User user = new User(); // User : IBase
    bool isValid = f(user);

    I also understood that you would like to separate the Linq expression "f" into a Func<IBase, bool> part and a Func<User, bool> part, similar to

    Func<IBase, bool> f1 = ibase => !string.IsNullOrEmpty(ibase.ID);
    Func<User, bool> f2 = user => !string.IsNullOrEmpty(user.Name);
    User user = new User();
    bool isValid = f1(user) && f2(user);
    Is this correct?



    Thursday, June 3, 2010 2:17 PM