網(wǎng)站首頁 編程語言 正文
Join子句
一、簡介
使用join子句可以將來自不同源序列并且在對象模型中沒有直接關系的元素相關聯(lián),唯一的要求是每個源中的元素需要共享某個可以進行比較以判斷是否相等的值,join子句使用特殊的equals關鍵字比較指定的鍵是否相等。
二、案例
內(nèi)部連接
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中執(zhí)行左外連接,請將DefaultIfEmpty方法與分組連接結合起來,以指定要在某個元素不具有匹配元素時產(chǎn)生的默認右側元素,可以使用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之后,會把數(shù)據(jù)查出來在內(nèi)存中操作
如果不去重,用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
相關推薦
- 2023-01-28 Flutter?Widget移動UI框架使用Material和密匙Key實戰(zhàn)_Android
- 2022-04-20 appium中常見的幾種點擊方式_python
- 2023-01-07 Android實現(xiàn)簡單的自定義ViewGroup流式布局_Android
- 2022-07-27 C++詳細講解圖的拓撲排序_C 語言
- 2023-02-09 go?sync?Waitgroup數(shù)據(jù)結構實現(xiàn)基本操作詳解_Golang
- 2022-03-20 .NET+Sqlite支持加密的操作方法_實用技巧
- 2022-06-14 Docker?配置容器固定IP的方法_docker
- 2022-07-09 apt報錯Hash 校驗和不符解決辦法
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支