網站首頁 編程語言 正文
Join子句
一、簡介
使用join子句可以將來自不同源序列并且在對象模型中沒有直接關系的元素相關聯,唯一的要求是每個源中的元素需要共享某個可以進行比較以判斷是否相等的值,join子句使用特殊的equals關鍵字比較指定的鍵是否相等。
二、案例
內部連接
var innerJoinQuery =
from category in categories
join prod in products on category.ID equals prod.CategoryID
select new { ProductName = prod.Name, Category = category.Name };
分組連接
var innerGroupJoinQuery =
from category in categories
join prod in products on category.ID equals prod.CategoryID
into prodGroup
select new { CategoryName = category.Name, Products = prodGroup };
左外部連接
var leftOuterJoinQuery =
from category in categories
join prod in products on category.ID equals prod.CategoryID
into prodGroup
from item in prodGroup.DefaultIfEmpty(new Product{Name =
string.Empty, CategoryID = 0})
select new { CatName = category.Name, ProdName = item.Name };
分析:
在左外連接中,將返回左側源序列中的所有元素,即使它們在右側序列中沒有匹配的元素也是如此。
若要在Linq中執行左外連接,請將DefaultIfEmpty方法與分組連接結合起來,以指定要在某個元素不具有匹配元素時產生的默認右側元素,可以使用null作為任何引用類型的默認值。也可以指定用戶定義的默認類型。
UNION子句
一、簡介
Union返回并集,并集是指位于兩個集合中任一集合的唯一的元素(自動去重復了)。在LINQ中UNION默認是去重的,沒有UNION ALL 語句,不去重用CONCAT()。
二、案例
1.查詢語句寫法
Union會去除重復項,相當于SQL的Union
var q = (from c in db.Customers
select c.Country
).Union(from e in db.Employees
select e.Country
);
相當于
var q1 = from s in db.Student
where s.ID < 3
select s;
var q2 = from s in db.Student
where s.ID < 5
select s;
//去掉重復的
var q = q1.Union(q2);
var r = q.ToList();//ToList之后,會把數據查出來在內存中操作
如果不去重,用Concat()
//Concat不會去除重復項目,相當于SQL的Union All;
//不去掉重復的 相當于union all,
var q3 = q1.Concat(q2);
var r3 = q3.ToList()
2.另一種寫法
var q = db.Customers.Union(db.Employees).select(d=>d.Country);
原文鏈接:https://www.cnblogs.com/wml-it/p/14837159.html
相關推薦
- 2022-04-08 一起來看看五條Python中的隱含特性_python
- 2022-02-27 Error in render: “TypeError: Cannot read propertie
- 2023-02-03 VB十七種可用一行代碼完成判斷的技巧代碼_vb
- 2023-07-26 TypeScript中的類型聲明declare
- 2022-05-04 使用Spring.Net框架實現多數據庫_實用技巧
- 2022-06-23 巧妙使用python?opencv庫玩轉視頻幀率_python
- 2022-03-20 Maven工程pom中如何定義jdk版本(maven配置jdk版本)
- 2022-07-17 SQL?Server中使用表變量和臨時表_MsSql
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支