开发者

How to write String.Contains in Dynamic Linq

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

T开发者_如何学Pythonhanks.


Found my answer now.

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


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜