How to build a LINQ query from text at runtime?

前端 未结 5 1654
时光说笑
时光说笑 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:55

    Although there may be some ways to do this, LINQ simply isn't designed for this scenario. Using CodeDOM (as Jon suggested) is probably the only way to get that easily done. If you trust the user and he/she has programming skills, you could perhaps just use old fashioned methods and let the user enter the query using SQL?

    If you, on the other hand, choose to create some visual tool for constructing queries, you don't need to build them by composing strings and you can compose expression trees instead. For example using Linq Kit and AsExpandable.

提交回复
热议问题