How to build a LINQ query from text at runtime?

前端 未结 5 1643
时光说笑
时光说笑 2021-01-02 03:15

I have a

class A {
   public int X;
   public double Y;
   public string Z;
   // and more fields/properties ...
};

and a List

5条回答
  •  攒了一身酷
    2021-01-02 03:45

    Answering it pretty late; though, it will help someone who visits this page.

    I had similar requirement and I solved it by dynamically compiling string as LINQ query, executing it over in-memory collection and collecting the result. Only catch is user input needs to be valid C# compile-able code else it returns an exception message instead of result.

    Code is pretty long so here is the github link

    Sample application on github shows multiple examples including projection.

提交回复
热议问题