Nhibernate 3.0 cookbook學習筆記 查詢


利用上一節的數據:Nhibernate 3.0 cookbook學習筆記 一對多與多對多映射 我們來做查詢

1 Criteria Queries

簡單的單表查詢:

 public IEnumerable<Movie> GetMoviesDirectedBy(string directorName)
        {
            return _session.CreateCriteria(typeof(Movie))
            .Add(Restrictions.Eq("Director", directorName))
            .List<Movie>();
        }

設置返回最多結果,類似Top:

public IEnumerable<Movie> GetMovies()
        {
            return _session.CreateCriteria<Movie>()
                .SetMaxResults(10).List<Movie>();
        }

添加排序:

public IEnumerable<Movie> GetMovies()
        {
            return _session.CreateCriteria<Movie>()
                .AddOrder(Order.Desc("Name"))
                .List<Movie>();
        }

使用Between,有兩種方法:

 public IEnumerable<Product> GetProductByPrice(decimal minPrice, decimal maxPrice)
        {
            return _session.CreateCriteria<Product>()
                .Add(Restrictions.Between("UnitPrice", minPrice, maxPrice))
                .List<Product>();
            //也可以用下面這種方法
            //return _session.CreateCriteria<Product>()
            //.Add(Restrictions.And(
            //Restrictions.Ge("UnitPrice", minPrice),
            //Restrictions.Le("UnitPrice", maxPrice)
            //))
            //.List<Product>();
        }

使用Like:

 public IEnumerable<Movie> GetMoviesDirectedBy(string directorName)
        {
            return _session.CreateCriteria(typeof(Movie))
            .Add(Restrictions.Like("Director", directorName,MatchMode.Anywhere))
            .List<Movie>();
        }

 返回唯一結果:

public Book GetBookByISBN(string isbn)
        {
            return _session.CreateCriteria<Book>()
            .Add(Restrictions.Eq("ISBN", isbn))
            .UniqueResult<Book>();
        }

分組GroupBy:

public IEnumerable<object[]> GetMovieGroupByDirector()
        {
            return _session.CreateCriteria<Movie>()
                .SetProjection(Projections.GroupProperty("Director").As("director"),
                Projections.GroupProperty("UnitPrice").As("UnitPrice")).
                AddOrder(Order.Asc("director")).List<object[]>();
        }

 統計Count:

public int GetMovieCount()
        {
            return  (int)_session.CreateCriteria<Movie>()
                .SetProjection(Projections.RowCount()).UniqueResult();
        }

In:

public IEnumerable<Movie> GetMoviesWithDirectors()
        {
            return _session.CreateCriteria<Movie>()
                .Add(Restrictions.In("Director", new string[] { "寧浩", "Steven Spielberg" })).List<Movie>();
        }

多表查詢:

public IEnumerable<Movie> GetMoviesWith(string actorName)
        {
            return _session.CreateCriteria<Movie>()
                .CreateCriteria("ActorRoles", JoinType.InnerJoin)
                .Add(Restrictions.Eq("Actor", actorName))
                .List<Movie>();
        }

 PS:上面查詢中的"Director","Name"等字段都是對應類中的屬性,而不是表名或表中的字段。

 2 QueryOver

QueryOver允許我們用lambda表達式來執行查詢。

簡單的單表查詢:

 public IEnumerable<Movie> GetMoviesDirectedBy(string directorName)
        {
            return _session.QueryOver<Movie>()
            .Where(m => m.Director == directorName)
            .List();
        }

Top:

_session.QueryOver<Movie>().Take(10).List<Movie>();

OrderBy:

_session.QueryOver<Movie>().OrderBy(m => m.Name).Desc.List<Movie>();

Between:

 public IEnumerable<Product> GetProductByPrice(decimal minPrice,decimal maxPrice)
        {
            return _session.QueryOver<Product>()
            .Where(p => p.UnitPrice >= minPrice
            && p.UnitPrice <= maxPrice)
            .OrderBy(p => p.UnitPrice).Asc
            .List();
        }

Between也可以用下面這個方法:

 public IEnumerable<Product> GetProductByPrice(decimal minPrice,decimal maxPrice)
        {
            return _session.QueryOver<Product>().WhereRestrictionOn(p => p.UnitPrice)
                .IsBetween(minPrice).And(maxPrice).List();
        }

Like:

_session.QueryOver<Movie>().WhereRestrictionOn(m => m.Name).IsLike("The Bucket List").List<Movie>();

返回唯一結果:

 public Book GetBookByISBN(string isbn)
        {
            return _session.QueryOver<Book>()
            .Where(b => b.ISBN == isbn)
            .SingleOrDefault();
        }

Count:

 _session.QueryOver<Movie>().RowCount();

In:

_session.QueryOver<Movie>().And(Restrictions.On<Movie>(m => m.Director).IsIn(new string[] { "寧浩", "Steven Spielberg" })).List<Movie>();

多表查詢:

public IEnumerable<Movie> GetMoviesWith(string actorName)
        {
            return _session.QueryOver<Movie>() .OrderBy(m => m.UnitPrice).Asc
                .Inner.JoinQueryOver<ActorRole>(m=>m.ActorRoles).
                Where(a => a.Actor == actorName).List();  
        }

3 Hibernate Query Language

HQL有點類似於SQL,我們可以寫出類似SQL的語句來執行查詢

簡單查詢:

 public IEnumerable<Movie> GetMoviesDirectedBy(string directorName)
        {
            var hql = @"from Movie m where m.Director = :director";
            return _session.CreateQuery(hql)
            .SetString("director", directorName)
            .List<Movie>();
        }

Top:

var hql = @"select  m from Movie m";
            return _session.CreateQuery(hql).SetMaxResults(10).List<Movie>();

OrderBy:

var hql = @"select  m from Movie m order by m.Name desc";
            return _session.CreateQuery(hql).List<Movie>();

Between:

 public IEnumerable<Product> GetProductByPrice(decimal minPrice,decimal maxPrice)
        {
            var hql = @"from Product p where p.UnitPrice >= :minPrice and p.UnitPrice <= :maxPrice order by p.UnitPrice asc";
            return _session.CreateQuery(hql)
            .SetDecimal("minPrice", minPrice)
            .SetDecimal("maxPrice", maxPrice)
            .List<Product>();
        }

唯一值,也可以在sql中加distinct :

 public Book GetBookByISBN(string isbn)
        {
            var hql = @"from Book b where b.ISBN = :isbn";
            return _session.CreateQuery(hql)
            .SetString("isbn", isbn).UniqueResult<Book>();
        }

Group by:

var hql = @"select  m.Name,count(m.UnitPrice) from Movie m Group by m.Name";
            return _session.CreateQuery(hql).List<object[]>();

多表查詢:

 public IEnumerable<Movie> GetMoviesWith(string actorName)
        {
            var hql = @"select m from Movie m inner join m.ActorRoles as ar where ar.Actor = :actorName";
            return _session.CreateQuery(hql)
            .SetString("actorName", actorName)
            .List<Movie>();
        }

 

代碼下載:點我

 

 


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com