score:3

Accepted answer

aaah, i see what you are asking... and you're really entering some very murky waters, one of the few areas that the .net reflection libraries aren't beautiful to work with. you have to create a call expression to call queryable.where() on the queryabledata object, and create a new query using that expression... the problem is that getting a generic version of a method in .net isn't necessarily the easiest thing you've ever run across in your life:

methodcallexpression call = expression.call(
    null, // calling queryable.where(), extension method, not instance method
    getgenericmethod<string>(typeof(queryable), "where", typeof(iqueryable<string>), typeof(expression<func<string,bool>>)),
    expression.constant(queryabledata),
    expression.lamda(
       e1,
       p1)
);
iqueryable<string> res = queryabledata.provider.createquery<string>(call);

you would also have to define getgenericmethod (you can find better implementations for this online at other places, this is really quite a simple approach):

private static methodinfo getgenericmethod<t>(type type, string name, params type[] paramtypes)
{
    methodinfo[] methods = type.getmethods(name);
    foreach(methodinfo mi in methods)
    {
        if(!mi.isgenericmethoddefinition) // or some similar property
            continue;
        if(mi.getgenericarguments().length != 1)
            continue;
        if(mi.getparameters().length != paramtypes.length)
            continue;


        methodinfo genmethod = mi.makegenericmethod(new type[]{typeof(t)});
        var ps = genmethod.getparameters();
        bool isgood = true;
        for(int i = 0; i < ps.length; i++)
        {
            if(ps[i].parametertype != paramtypes[i])
            {
               isgood = false;
               break;
            }
        }

        if(isgood)
            return genmethod;

    }

    return null;
}

there are almost undoubtedly a few errors in there, but i hope you can see where to go from there...

score:0

just to give my solution:

string[] arr = {"s1","s2","s3"};
iqueryable<string> queryabledata = arr.asqueryable<string>();
parameterexpression pe = expression.parameter(typeof(string), "company");
expression right = expression.constant("on");
expression left = expression.call(pe, typeof(string).getmethod("contains"), right);
methodcallexpression e2 = expression.call(
            typeof(queryable),
            "where",
            new type[] { queryabledata.elementtype },
            queryabledata.expression,
            expression.lambda<func<string, bool>>(left, new parameterexpression[] { pe }));

iqueryable<string> res = queryabledata.provider.createquery<string>(e2);

Related Query

More Query from same tag