问题
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