常用查询:
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
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