LING与HQL(三)
生活随笔
收集整理的這篇文章主要介紹了
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
????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
轉載于:https://www.cnblogs.com/pojia/archive/2008/03/14/1106270.html
總結
以上是生活随笔為你收集整理的LING与HQL(三)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一个ASP.NE导出Excel类
- 下一篇: 蓝桥杯第六届国赛JAVA真题----表格