Linq for NHibernate - filtering on <many-to-one> foreign key causes extra lookup

ぃ、小莉子 提交于 2020-01-23 17:55:09

问题


Trying out Linq for NHibernate and first noticed this:

    var list1 = (from ut in session.Linq<UserThings>()
    where ut.User.ID == 10
    select ut).ToList();
    var list2 = session.CreateCriteria(typeof (UserThings), "ut")
    .Add(Expression.Eq("ut.User.ID", 10))
    .List<UserThings>();

This first query will join on the 'User' table, but the second will not. Somehow the second knows that UserID is the foreign key and that it doesn't need to perform the join to filter.


回答1:


The best piece of advice anyone will give you is don't use Linq2NH unless you are writing a very basic, home-made application.

Its very immature, its slow, only supports basic queries, doesn't support caching, eager loading, grouping.... I spent 6 months using it before I abandoned it and made the effort to learn HQL/Criteria.




回答2:


@reach4thelasers, re: cachable and fetch mode, I thought you could do things like the following, no?

var uthings = session.Linq<UserThings>();
uthings.QueryOptions.SetCachable(true);
uthings.QueryOptions.RegisterCustomAction(c => c.SetFetchMode("/UserThing/User", FetchMode.Eager))
return uthings.Where(ut => ut.User == user);

Instead of the not-so-beautiful RegisterCustomAction+SetFetchMode you can also do uthings.Expand("User") but that only works with single property, can't combine two Expands until NHLQ-35 is fixed.



来源:https://stackoverflow.com/questions/1677301/linq-for-nhibernate-filtering-on-many-to-one-foreign-key-causes-extra-lookup

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!