3

I want to know which is the best way to build dynamic queries in LINQ. Queries will be complex and nested. While searching I found a few ways:

  1. Linq dynamic (System.Linq.Dynamic)
  2. Albahari's Predicate builder class
  3. Linq.Expression

There may be more options than these. Which is the best way?

bobbymcr
  • 23,769
  • 3
  • 56
  • 67
Red Swan
  • 15,157
  • 43
  • 156
  • 238
  • 2
    This really depends on your requirements. Surely it's possible to use these any of them. Please tell us more of what you're intentions are – Polity Nov 28 '11 at 04:08
  • This might help: http://stackoverflow.com/questions/1782577/what-is-the-best-approach-to-build-dynamic-linq-queries – Mamta D Nov 28 '11 at 04:10
  • @Polity: Agreed! but as of now think about need to build the Search engine based on Dynamic Linq. so which is the best way... – Red Swan Nov 28 '11 at 09:46
  • @Red Swan, again, do you want to allow your users to type in linq expressions or do you want to append some predefined rules based on the configuration of the user? – Polity Nov 28 '11 at 09:48
  • @Polity, I want to build the queries which may have the joins dynamically. so to concentrate on append the where conditions won't enough. – Red Swan Dec 02 '11 at 07:23

2 Answers2

1

This depends on your circumstances: how fast do you need it, what is your starting point, and so on. In an unconstrained world, I think the best thing is to roll your own library for building dynamic queries. You can use Scott's or Joseph's work as an inspiration, but in the end it all "bottoms out" in the Linq.Expression library.

One advantage to the "do it yourself" approach is that you would not need to bridge from your code to someone's framework. Rather, you would code directly to .NET APIs. This may be useful when you already have a representation of your dynamic queries, for example, in a model that you present to users through a UI, in an XML file, etc. All you need is to walk that representation recursively, and produce System.Linq.Expression as the return.

FWIW, my company took this approach when .NET 3.5 came out, and we are very happy with the outcome.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • For sure I am planning to build the own library for the same. but as of now reqirement priority on high to build the search engine. i as of now i am thinking about to quick use existing source code for the same. i wanted to know which is the best one out of given in question... any corrections and suggessions will appraciable... – Red Swan Nov 28 '11 at 09:42
  • @RedSwan If you need to build something quickly, Linq.Dynamic should do the trick: [it lets you build queries from strings](http://stackoverflow.com/questions/848415/linq-dynamic-where-clause) so all you need to provide is a string representation of your search condition. – Sergey Kalinichenko Nov 28 '11 at 13:42
-1

Linq queries can be written in two ways and let you use any kind of nesting.

Query Syntax

IEnumerable<int> numQuery1 = 
        from num in numbers
        where num % 2 == 0
        orderby num
        select num;

Method Syntax

 IEnumerable<int> numQuery2 = numbers.Where(num => num % 2 == 0).OrderBy(n => n);

For more information about Linq, you can visit Microsoft's LINQ (Language-Integrated Query). It contains everything from getting started to sample tutorials

Pankaj Upadhyay
  • 12,966
  • 24
  • 73
  • 104
  • @Pankaj: Thanks for answer Pankaj. in fact the answer given by you is really not related to question. i wanted to bild the search engine. I know how to use orderby and other clauses, but unfortunatly can not build the Linq query dynamically ... – Red Swan Nov 28 '11 at 09:45