欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

linq中的串聯(lián)操作符

 更新時間:2022年03月10日 09:53:31   作者:.NET開發(fā)菜鳥  
這篇文章介紹了linq中的串聯(lián)操作符,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

串聯(lián)是一個將兩個集合連接在一起的過程。在Linq中,這個過程通過Concat操作符實現(xiàn)。Concat操作符用于連接兩個集合,生成一個新的集合。來看看Concat操作符的定義:

public static IEnumerable<TSource> Concat<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second)

 從方法定義中可以看出:第二個參數(shù)為輸入一個新的集合,與調(diào)用集合連接,生成并返回一個新的集合。

注意:

第一個集合和第二個集合的元素的類型必須是相同的。

請看下面的例子:

定義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()方法中調(diào)用:

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)
        {
            // 初始化數(shù)據(jù)
            List<Category> listCategory = new List<Category>()
            {
              new Category(){ Id=1,CategoryName="計算機",CreateTime=DateTime.Now.AddYears(-1)},
              new Category(){ Id=2,CategoryName="文學(xué)",CreateTime=DateTime.Now.AddYears(-2)},
              new Category(){ Id=3,CategoryName="高校教材",CreateTime=DateTime.Now.AddMonths(-34)},
              new Category(){ Id=4,CategoryName="心理學(xué)",CreateTime=DateTime.Now.AddMonths(-34)}
            };

            List<Category> list = new List<Category>()
            {
              new Category(){ Id=5,CategoryName="管理類",CreateTime=DateTime.Now.AddDays(-34)},
              new Category(){ Id=6,CategoryName="金融學(xué)",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();
        }
    }
}

結(jié)果:

如何輸出不同集合中相同類型的元素呢?看下面的例子:

在定義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)
        {
            // 初始化數(shù)據(jù)
            List<Category> listCategory = new List<Category>()
            {
              new Category(){ Id=1,CategoryName="計算機",CreateTime=DateTime.Now.AddYears(-1)},
              new Category(){ Id=2,CategoryName="文學(xué)",CreateTime=DateTime.Now.AddYears(-2)},
              new Category(){ Id=3,CategoryName="高校教材",CreateTime=DateTime.Now.AddMonths(-34)},
              new Category(){ Id=4,CategoryName="心理學(xué)",CreateTime=DateTime.Now.AddMonths(-34)}
            };
            List<Product> listProduct = new List<Product>()
            {
               new Product(){Id=1,CategoryId=1, Name="C#高級編程第10版", Price=100.67,CreateTime=DateTime.Now},
               new Product(){Id=2,CategoryId=1, Name="Redis開發(fā)和運維", 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="高等數(shù)學(xué)", 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();
        }
    }
}

結(jié)果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • ASP.net做的IP訪問限制

    ASP.net做的IP訪問限制

    ASP.net做的IP訪問限制...
    2006-09-09
  • C# 命名規(guī)則(挺不錯的)

    C# 命名規(guī)則(挺不錯的)

    我自己總結(jié)的一套命名規(guī)則,其實規(guī)則很重要,它是一種標(biāo)準(zhǔn),可有可無,但有總會比無好,大家正在編碼的同志仔細看看,給點改進意見。
    2009-02-02
  • 深入講解.Net Core中的Api版本控制

    深入講解.Net Core中的Api版本控制

    這篇文章主要給大家介紹了關(guān)于.Net Core中Api版本控制的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-10-10
  • 通過ASP.net實現(xiàn)flash對數(shù)據(jù)庫的訪問

    通過ASP.net實現(xiàn)flash對數(shù)據(jù)庫的訪問

    近來網(wǎng)站需要在flash中提取數(shù)據(jù)庫中的數(shù)據(jù),從網(wǎng)上找了一點資料,今天下午在自己的機器上實現(xiàn)了一下,還是比較簡單的。
    2009-08-08
  • 如何在ASP.Net Core中使用 IHostedService的方法

    如何在ASP.Net Core中使用 IHostedService的方法

    這篇文章主要介紹了如何在ASP.Net Core中使用 IHostedService的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • 強烈推薦一個基于.Net Framework開發(fā)的Windows右鍵菜單管理工具

    強烈推薦一個基于.Net Framework開發(fā)的Windows右鍵菜單管理工具

    這篇文章主要介紹了推薦一個基于.Net Framework開發(fā)的Windows右鍵菜單管理工具,今天給大家推薦一個Windows右鍵菜單管理工具,方便我們管理我們的右鍵菜單,需要的朋友可以參考下
    2023-05-05
  • Json.net 常用使用小結(jié)(推薦)

    Json.net 常用使用小結(jié)(推薦)

    下面小編就為大家?guī)硪黄狫son.net 常用使用小結(jié)(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06
  • ASP.NET實現(xiàn)上傳Excel功能

    ASP.NET實現(xiàn)上傳Excel功能

    本文主要介紹了ASP.NET 實現(xiàn)上傳EXCEL,利用NOPI操作,轉(zhuǎn)換得到DataTable的相關(guān)知識。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-03-03
  • asp.net 圖片的讀寫入庫實現(xiàn)代碼

    asp.net 圖片的讀寫入庫實現(xiàn)代碼

    asp.net對圖片的讀寫,實現(xiàn)將圖片保存到數(shù)據(jù)庫中,然后再讀取顯示的實現(xiàn)代碼。
    2009-11-11
  • MVC生成頁碼選擇器返回HTML代碼詳解

    MVC生成頁碼選擇器返回HTML代碼詳解

    這篇文章主要為大家詳細介紹了MVC生成頁碼選擇器返回HTML代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08

最新評論