zoukankan      html  css  js  c++  java
  • LING与HQL(三)

    常用查询:
        LINQ:
          from c in ctx.Customers select c
        HQL:
           from Customers c 
      比较一下,区别还是有的 ;注意HQL中Customers 是个领域模型;LINQ中的ctx是DataContext实例;
      既然他们都是对象化的查询,那下面的例子:
         LINQ:
          from c in ctx.Customers
             where c.ContractorName == ”tom“  select new {obj = c.ContractorName}
        HQL:
           from Customers c where c.ContractorName = ?   
          // "?"表示传入的参数 ,上面的两个例子中的c都可以看成一个对象,
      在如:
          LINQ:
           from c in ctx.Customers 
               where c.Orders.Count > 0 select c
            //Customers 和 Orders 是主外键关系
          HQL:
           from Orders o where o. Customers.ContractorName = ?  
           //o表示Orders这个对象,它的属性Customers也是一个对象
          select Customers from Orders o where o. Customers.ContractorName = ?  
          //在HQL中可以通过select 返回其他对象

    使用排序:
         LINQ:
            from c in ctx.Employees 
               where c.Employees.Count >0
               orderby c.EmployeeID descending , c.ReportsTo ascending 
               select c
          注意,因为Employees 有个自链接,[ReportsTo] REFERENCES [EmployeeID],所以有c.Employees.Count >0
    这个;
         HQL:
            from Customers c order by c.EmployeeID desc, c.ReportsTo asc
        这里HQL的排序方式和T-SQL一样;

    使用分组:
        简单分组:
        LINQ:
          from c in ctx.Employees
            group c by c.ReportsTo
          select c
        HQL:
          from Employees e group by e.EmployeeID
      带条件的分组:
         LINQ:
          from c in ctx.Employees
            group c by c.ReportsTo into tmp
            where tmp.Count() > 0
          select new { 分组数 = tmp.Count()}
        HQL:
          select count(e) from Employees e
             group by e.EmployeeID 
             having  e.EmployeeID = 11
  • 相关阅读:
    材料用词积累
    SqlServer 数据库/数据表 拆分(分布式)【转】
    SqlServer 数据库读写分离【转】
    (整理)在REHL6.5上部署ASP.NET MVC
    (整理)MySQL_REHL6.5 安装MySQL5.5
    (转)查看SQLServer最耗资源时间的SQL语句
    (转)SQLServer查询数据库各种历史记录
    (转)SqlServer2008 数据库同步:发布、订阅
    (整理)SQL Server 2008 CDC 功能使用
    (整理)EF分页的实现
  • 原文地址:https://www.cnblogs.com/pojia/p/1106270.html
Copyright © 2011-2022 走看看