Passing a Linq expression as a string?

前端 未结 3 1001
死守一世寂寞
死守一世寂寞 2021-01-14 03:20

The following code works fine

        using (var ctx = new MyEntities())
        {
            var devices = ctx.Devices
               .Where(x=> x.Devic         


        
相关标签:
3条回答
  • 2021-01-14 03:54

    I don't think Where can take a string as parameter. Dynamic Linq lets you pass queries as strings, though probably not in the specific format you're trying to do above. Depending on what exactly it is you are trying to achieve, it might be worth a look.

    0 讨论(0)
  • 2021-01-14 04:00

    You have to build the Expression manually.

    IIRC, there is a DynamicExpressions library in the LINQ101 samples that can do this for you.

    0 讨论(0)
  • 2021-01-14 04:05

    The dynamic linq sample can do much of this, except you drop the lambda notation:

    String expression = "Device == \"TEST\"" ;
    
    //... etc
    
        .Where(expression)
    

    Another example (from the blog):


    (source: scottgu.com)

    0 讨论(0)
提交回复
热议问题