tags:

views:

90

answers:

2

I'm trying to write a dynamic linq query like:

var q = obj.Where("message.Contains('hello')");

I know it works for

var q = obj.Where(o => o.message.Contains('hello'));

but i'm looking for dynamic linq solution

Thanks.

A: 

I know this isn't what you are looking for, but just as a point to consider:

Depending on how many various kinds of operation you expect to perform, I would create a switch statement to handle this.

As an example, some pseudocode using an enum:

(OperationType is an Enum if desired)

private object example(OperationType optype, Object obj, String match)
{
   var q;
   switch (optype)
        {
        case OperationType.Contains:
            q = obj.Where(o => o.message.Contains(match));
        break;
        case OperationType.EndsWith:
            q = obj.Where(o => o.message.EndsWith(match));
        break;
        case OperationType.StartsWith:
            q = obj.Where(o => o.message.StartsWith(match));
        break;
    }
    return q;
}
JYelton
Hi JYelton, thanks for your reply. Actually I'm looking for some answers like what Mike proposed. But Mike's expression doesnt work.
Zalan
+1  A: 

Found my answer now.

var q = obj.Where("message.Contains(@0)", "hello");
Zalan