What is the difference between JoinQueryOver and JoinAlias?

落爺英雄遲暮 提交于 2019-11-26 15:27:01

问题


I need to know what is the difference between JoinQueryOver and JoinAlias, and when to use each?

thanks.


回答1:


Functionally they do the same thing, create a join to another entity. The only difference is what they return. JoinQueryOver returns a new QueryOver with with the current entity being the entity joined, while JoinAlias returns the original QueryOver that has the current entity as the original root entity.

Whichever one you use is a matter of personal taste: (from http://nhibernate.info/doc/nh/en/index.html#queryqueryover)

IQueryOver<Cat,Kitten> catQuery =
    session.QueryOver<Cat>()
        .JoinQueryOver<Kitten>(c => c.Kittens)
            .Where(k => k.Name == "Tiddles");

and

Cat catAlias = null;
Kitten kittenAlias = null;
IQueryOver<Cat,Cat> catQuery =
    session.QueryOver<Cat>(() => catAlias)
        .JoinAlias(() => catAlias.Kittens, () => kittenAlias)
        .Where(() => kittenAlias.Name == "Tiddles");

Are functionally the same. Note how the kittenAlias is expressly referenced in the second query.




回答2:


QueryOver Series - Part 2: Basics and Joining by Andrew Whitaker gives a very good explanation:

Summary:

  • IQueryOver is a generic type with two type parameters TRoot and TSubType
  • .Select operates on TRoot while other QueryOver methods operate on TSubType.
  • TRoot stays the same as you’re building a query, but TSubType changes when you join using JoinQueryOver
  • JoinQueryOver and JoinAlias add joins to your query. JoinAlias doesn’t change TSubType, but JoinQueryOver does.
  • You can use aliases when building a query to refer to properties that don’t belong to TRoot or TSubType


来源:https://stackoverflow.com/questions/5416560/what-is-the-difference-between-joinqueryover-and-joinalias

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