What is the difference between NHibernate Query<> vs QueryOver<>?

后端 未结 2 816
生来不讨喜
生来不讨喜 2020-12-29 19:54

I just started with NHibernate (using SQLite) in my current project and I mostly used Query<>, because I was familiar writing db queries in Linq.

2条回答
  •  天涯浪人
    2020-12-29 20:13

    There are plenty of answers regarding QueryOver versus Query here on Stackoverflow but in a nutshell:-

    QueryOver is a strongly-typed version of Criteria, and is more NHibernate specific. Pretty much anything you can do in ICriteria can be done with QueryOver. In the golden days of ICriteria NH2 you always had to cast, hence this is why now you need to cast at the end of the chain back to an int.

    LINQ (Query) is a standard query method that works on IQueryable that doesn't need explicit references to NHibernate and can be considered more ORM agnostic and therefore follows the linq standard. As you rightly pointed out you do not need to cast to an int as you are selecting into the result the customNumber.

    I would be very surprised for your simple example if the generated SQL was very different.

    I was a big fan of QueryOver but as the Linq provider is getting more mature then 95% of my queries I use Query but for some Nhibernate specific stuff I resort back down to QueryOver. Either way I recommend using a profiling tool to see what you can live with.

    Refs: Tradeoffs or versus and versus

提交回复
热议问题