NHibernate QueryOver is not a Linq provider (that is how to do join with QueryOver API)

Starting from NHibernate 3.0 a new API was build as a wrapper around the ICriteria object.

Obviously I’m talking about the powerful API called QueryOver. With this API you can build a wide range of query object, from the simplest to the more complicated one. But you need to remember that this API is not a Linq provider, and then you cannot apply those constructs typically applied in a Linq query. I explain with an example what I really mean.

Introduction: you have a data model with 2 table, Category and Customer, with a one-to-many relationship between them.

With Entity Framework and Linq To Entities it’s pretty simple to query data that belong to both entities, thanks to navigation properties and Linq provider, i.e. all customers that belong to a category with a specified Id, as shown in this example:

var customerList = customers.Where(c => c.CategoryId == 1);

If you try to execute the same query with the same where conditions applied to a QueryOverObject, like this example:

QueryOver<Customer>; qu = QueryOver.Of<Customer>()
      .Where(c => c.CategoryId == 1);

This code throws a NHibernateException saying that “could not resolve property: Category.Id of : MyNamespace.Customer”, suggesting to verify the mapping as property “Category.Id” could not be found”.

Obviously such a property doesn’t exist at all, it’s only the QueryOver API that concatenates the Navigation property and and its field named Id (in this example).

This means that: you cannot make a query with QueryOver API that refers to fields belonging to a navigation property in where clause….without explicitly defying a join between the object itself and its navigation property. An example will help to understand better.

Category cat = null;
QueryOver<Customer> query = QueryOver.Of<Customer>()
 .JoinAlias(x => x.Category, () =>; cat)
 .Where(x => cat.Id == 1);

I have defined a join with the JoinAlias method, which uses an alias (the Category object declared two rows before) to project the object under the navigation property. After that you can use this alias inside the others method (such as “Where” in this example) to refer to the navigation property field (cat.Id).

As you can see, the way you write the lambda expression inside a Linq provider’s where clause is quite different than the “”Where” condition of a QueryObject object.

Not even the logical operator “And” can be used in the same way. To apply this logical operator to the “where” filter you have to use the “And” method, as shown here:

Category cat = null;
QueryOver<Customer> query = QueryOver.Of<Customer>()
   .JoinAlias(x => x.Category, () => cat)
   .Where(x => cat.Id == 1)
   .And(x => cat.IsEnabled);

247 thoughts on “NHibernate QueryOver is not a Linq provider (that is how to do join with QueryOver API)

  1. Pingback: viagra canada
  2. Pingback: canada pharmacy
  3. Pingback: mazhor4sezon
  4. Pingback: canadian drugs
  5. Pingback: filmfilmfilmes
  6. Pingback: viagra canada
  7. Pingback: gRh9UPV
  8. Pingback: canada rx
  9. Pingback: 9-05-2022
  10. Pingback: kinoteatrzarya.ru
  11. Pingback: TopGun2022
  12. Pingback: Xvideos
  13. Pingback: XVIDEOSCOM Videos
  14. Pingback: drugstore online
  15. Pingback: northwestpharmacy
  16. Pingback: ivanesva
  17. Pingback: Northwest Pharmacy
  18. Pingback: buy viagra 25mg
  19. Pingback: dkyubn.bizwebs.com
  20. Pingback: canadian pharmacys
  21. Pingback: canadian viagra
  22. Pingback: Netflix
  23. Pingback: FILM
  24. Pingback: designchita.ru
  25. Pingback: YA-krasneyu
  26. Pingback: design-human.ru
  27. Pingback: designmsu.ru
  28. Pingback: vkl-design.ru
  29. Pingback: irida-design.ru
  30. Pingback: projectio
  31. Pingback: psy online
  32. Pingback: canadian drugstore
  33. Pingback: uels ukrain
  34. Pingback: Northwest Pharmacy
  35. Pingback: kwersv.proweb.cz
  36. Pingback: canada pharmacies
  37. Pingback: DPTPtNqS
  38. Pingback: qQ8KZZE6
  39. Pingback: D6tuzANh
  40. Pingback: SHKALA TONOV
  41. Pingback: chelovek-iz-90-h
  42. Pingback: 3Hk12Bl
  43. Pingback: 3NOZC44
  44. Pingback: 01211
  45. Pingback: tor-lyubov-i-grom
  46. Pingback: film-tor-2022
  47. Pingback: hd-tor-2022
  48. Pingback: hdorg2.ru
  49. Pingback: JXNhGmmt
  50. Pingback: Psikholog
  51. Pingback: netstate.ru
  52. Pingback: Link
  53. Pingback: psy
  54. Pingback: bit.ly
  55. Pingback: cleantalkorg2.ru
  56. Pingback: bucha killings
  57. Pingback: War in Ukraine
  58. Pingback: Ukraine
  59. Pingback: site
  60. Pingback: stats
  61. Pingback: Ukraine-war
  62. Pingback: movies
  63. Pingback: gidonline
  64. Pingback: web
  65. Pingback: film.8filmov.ru
  66. Pingback: video
  67. Pingback: filmgoda.ru
  68. Pingback: rodnoe-kino-ru
  69. Pingback: stat.netstate.ru
  70. Pingback: sY5am
  71. Pingback: Dom drakona
  72. Pingback: JGXldbkj
  73. Pingback: aOuSjapt
  74. Pingback: ìûøëåíèå
  75. Pingback: psikholog moskva
  76. Pingback: Dim Drakona 2022
  77. Pingback: TwnE4zl6
  78. Pingback: psy 3CtwvjS
  79. Pingback: lalochesia
  80. Pingback: buy viagra online
  81. Pingback: online drug store
  82. Pingback: film onlinee
  83. Pingback: 3qAIwwN
  84. Pingback: stromectol generic
  85. Pingback: video-2
  86. Pingback: sezons.store
  87. Pingback: stromectol stock
  88. Pingback: psy-news.ru
  89. Pingback: ivermectine
  90. Pingback: 000-1
  91. Pingback: 3SoTS32
  92. Pingback: 3DGofO7
  93. Pingback: canadian pharmacys
  94. Pingback: canadian pharmacy
  95. Pingback: cialis from canada

Comments are closed.