網站首頁 編程語言 正文
串聯是一個將兩個集合連接在一起的過程。在Linq中,這個過程通過Concat操作符實現。Concat操作符用于連接兩個集合,生成一個新的集合。來看看Concat操作符的定義:
public static IEnumerableConcat (this IEnumerable first, IEnumerable second)
?從方法定義中可以看出:第二個參數為輸入一個新的集合,與調用集合連接,生成并返回一個新的集合。
注意:
第一個集合和第二個集合的元素的類型必須是相同的。
請看下面的例子:
定義Category類,其類定義如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SeriesOperation { public class Category { public int Id { get; set; } public string CategoryName { get; set; } public DateTime CreateTime { get; set; } } }
然后在Main()方法中調用:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SeriesOperation { class Program { static void Main(string[] args) { // 初始化數據 ListlistCategory = new List () { new Category(){ Id=1,CategoryName="計算機",CreateTime=DateTime.Now.AddYears(-1)}, new Category(){ Id=2,CategoryName="文學",CreateTime=DateTime.Now.AddYears(-2)}, new Category(){ Id=3,CategoryName="高校教材",CreateTime=DateTime.Now.AddMonths(-34)}, new Category(){ Id=4,CategoryName="心理學",CreateTime=DateTime.Now.AddMonths(-34)} }; List list = new List () { new Category(){ Id=5,CategoryName="管理類",CreateTime=DateTime.Now.AddDays(-34)}, new Category(){ Id=6,CategoryName="金融學",CreateTime=DateTime.Now.AddYears(-4)} }; // 查詢表達式 var newListExpress = (from c in listCategory select c).Concat(from p in list select p); Console.WriteLine("查詢表達式輸出:"); foreach (var item in newListExpress) { Console.WriteLine($"Id:{item.Id},CategoryName:{item.CategoryName},CreateTime:{item.CreateTime}"); } var newList = listCategory.Concat(list); Console.WriteLine("方法語法輸出:"); foreach (var item in newList) { Console.WriteLine($"Id:{item.Id},CategoryName:{item.CategoryName},CreateTime:{item.CreateTime}"); } Console.ReadKey(); } } }
結果:
如何輸出不同集合中相同類型的元素呢?看下面的例子:
在定義Product類,其定義如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SeriesOperation { public class Product { public int Id { get; set; } public int CategoryId { get; set; } public string Name { get; set; } public double Price { get; set; } public DateTime CreateTime { get; set; } } }
Category類中的CategoryName和Product類中的Name都是string類型的,在下面的例子中輸出Category中的CategoryName和Product中的Name。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SeriesOperation { class Program { static void Main(string[] args) { // 初始化數據 ListlistCategory = new List () { new Category(){ Id=1,CategoryName="計算機",CreateTime=DateTime.Now.AddYears(-1)}, new Category(){ Id=2,CategoryName="文學",CreateTime=DateTime.Now.AddYears(-2)}, new Category(){ Id=3,CategoryName="高校教材",CreateTime=DateTime.Now.AddMonths(-34)}, new Category(){ Id=4,CategoryName="心理學",CreateTime=DateTime.Now.AddMonths(-34)} }; List listProduct = new List () { new Product(){Id=1,CategoryId=1, Name="C#高級編程第10版", Price=100.67,CreateTime=DateTime.Now}, new Product(){Id=2,CategoryId=1, Name="Redis開發和運維", Price=69.9,CreateTime=DateTime.Now.AddDays(-19)}, new Product(){Id=3,CategoryId=2, Name="活著", Price=57,CreateTime=DateTime.Now.AddMonths(-3)}, new Product(){Id=4,CategoryId=3, Name="高等數學", Price=97,CreateTime=DateTime.Now.AddMonths(-1)}, new Product(){Id=5,CategoryId=6, Name="國家寶藏", Price=52.8,CreateTime=DateTime.Now.AddMonths(-1)} }; // 查詢表達式 var newList = (from p in listProduct select p.Name).Concat(from c in listCategory select c.CategoryName); Console.WriteLine("查詢表達式輸出:"); foreach (var item in newList) { Console.WriteLine(item); } Console.WriteLine("*************************"); // 方法語法 var newListFun = listProduct.Select(p => p.Name).Concat(listCategory.Select(c => c.CategoryName)); Console.WriteLine("方法語法輸出:"); foreach (var item in newListFun) { Console.WriteLine(item); } Console.ReadKey(); } } }
結果:
原文鏈接:https://www.cnblogs.com/dotnet261010/p/9311015.html
相關推薦
- 2022-12-13 詳解如何魔改Retrofit實例_Android
- 2022-05-06 嵌入式C語言輕量級程序架構內核編寫_C 語言
- 2022-05-26 Flutter自定義年月日倒計時_Android
- 2022-05-26 為Jenkins添加SSH全局憑證_相關技巧
- 2022-04-18 C#繪制餅狀圖和柱狀圖的方法_C#教程
- 2023-02-06 Python利用Pytorch實現繪制ROC與PR曲線圖_python
- 2022-04-25 Python如何生成exe文件?用Pycharm一步步帶你學(超詳細、超貼心)_python
- 2022-05-15 Python?文本文件與csv文件的讀取與寫入_python
- 最近更新
-
- 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同步修改后的遠程分支