zoukankan      html  css  js  c++  java
  • NHibernate系列文章二十四:NHibernate查询之Linq查询(附程序下载)

    摘要

    NHibernate从3.0开始支持Linq查询。写Linq to NHibernate查询就跟写.net linq代码一样,非常灵活,可以很容易实现复杂的查询。这篇文章使用Linq to NHibernate重写之前所有的查询。

    本篇文章的代码可以到NHibernate查询下载

    1、创建IQueryable对象,返回所有Customer对象信息

    1         public IList<Customer> QueryAllLinq()
    2         {
    3             return Session.Query<Customer>().ToList();
    4         }
    • 要在代码中添加对NHibernate.Linq的引用
    • IQueryable对象是延迟加载的
    • ToList方法表示立即执行,得到IList<Customer>集合

    2、创建别名

    1         public IList<Customer> QueryAllLinq()
    2         {
    3             return (from c in Session.Query<Customer>().ToList() select c).ToList();
    4         }

    3、指定对象返回数组

    1         public IList<int> SelectIdLinq()
    2         {
    3             var query = Session.Query<Customer>().Select(c => c.Id).Distinct().ToList();
    4             return query.ToList();
    5         }

    Distinct方法返回无重复项目的序列。

    4、添加查询条件

     1         public IList<Customer> GetCustomerByNameLinq(string firstName, string lastName)
     2         {
     3             return Session.Query<Customer>().Where(c => c.FirstName == firstName && c.LastName == lastName).ToList();
     4         }
     5 
     6         public IList<Customer> GetCustomersStartWithLinq()
     7         {
     8             var query = from c in Session.Query<Customer>() where c.FirstName.StartsWith("J") select c;
     9             return query.ToList();
    10         }

    5、order by

    1         public IList<Customer> GetCustomersOrderByLinq()
    2         {
    3             var query = from c in Session.Query<Customer>() orderby c.FirstName ascending select c;
    4             return query.ToList();
    5         }

    6、关联查询

     1         public IList<OrderCount> SelectOrderCountLinq()
     2         {
     3             var query = Session.Query<Customer>().Select(g => new OrderCount { CustomerId = g.Id, Count = g.Orders.Count() });
     4             return query.ToList();
     5         }
     6 
     7         public IList<Customer> GetCustomersOrderCountGreaterThanLinq()
     8         {
     9             var query = Session.Query<Customer>().Where(c => c.Orders.Count > 2);
    10             return query.ToList();
    11         }
    12 
    13         public IList<Customer> GetCustomersOrderDateGreatThanLinq(DateTime orderDate)
    14         {
    15             var query = Session.Query<Customer>().Where(c => c.Orders.Any(o => o.Ordered > orderDate));
    16             return query.ToList();
    17         }

    因为.net方法不能返回匿名类对象以及含有匿名类对象的对象,因此添加OrderCount类,SelectOrderCountLinq方法返回IList<OrderCount>对象。

    1     public class OrderCount
    2     {
    3         public int CustomerId { get; set; }
    4         public int Count { get; set; }
    5     }

    结语

    Linq to NHibernate基于.net Linq,非常灵活。.net Linq提供的所有集合操作,Linq to NHibernate也都提供了。使用它可以完成大部分NHibernate查询。下一篇文章介绍NHibernate 3.2的Query Over查询。

  • 相关阅读:
    一题多解 —— 同时找到序列的最大值最小值
    中位数与顺序统计量
    软件开发 —— 极限编程(XP:Extreme Programming)
    一题多解 —— 二项式分布的期望和方差的计算
    C++中explicit关键字用法
    【联系】二项分布的对数似然函数与交叉熵(cross entropy)损失函数
    随机变量统计独立性的相关证明
    PowerShell管理SCOM_批量设置维护模式(下)
    导出AD用户所属组,查询AD用户(aduser)
    SQL脚本运行
  • 原文地址:https://www.cnblogs.com/uncle_danny/p/5676263.html
Copyright © 2011-2022 走看看