Build dynamic LINQ?

前端 未结 4 1410
我寻月下人不归
我寻月下人不归 2020-12-21 00:45

I\'m using LINQ to query data. Consider a case where the user only wants to report on say 1 of the 3 fields? (see below)

Can anyone tell me how to build the query d

4条回答
  •  情话喂你
    2020-12-21 01:29

    Microsoft has created an example library (Linq Dynamic Library) that allows you to construct dynamic querys in Linq, you can see how to use it and download it from this link: http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

    That being said, you can also declare an IQueriable at the begining of your code, and add where clauses depending on parameters. I was going to write an example, but mattruma beat me to it :)

    I prefer the second option, but there are situations where using the Dynamic Library is a better solution.

提交回复
热议问题