最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

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

注意:

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

請看下面的例子:

定義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="計(jì)算機(jī)",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)}
            };

            // 查詢表達(dá)式
            var newListExpress = (from c in listCategory select c).Concat(from p in list select p);
            Console.WriteLine("查詢表達(dá)式輸出:");
            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="計(jì)算機(jī)",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ā)和運(yùn)維", 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)}
            };

            // 查詢表達(dá)式
            var newList = (from p in listProduct
                           select p.Name).Concat(from c in listCategory select c.CategoryName);
            Console.WriteLine("查詢表達(dá)式輸出:");
            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)文章

最新評論

荔波县| 磐安县| 丰镇市| 景洪市| 新晃| 河津市| 合肥市| 连云港市| 高州市| 沂南县| 门头沟区| 礼泉县| 叙永县| 波密县| 新丰县| 通化市| 两当县| 太湖县| 措勤县| 深圳市| 枣庄市| 岗巴县| 佛冈县| 鹤山市| 蒙自县| 和平区| 海盐县| 剑阁县| 德惠市| 安陆市| 加查县| 云和县| 西峡县| 贵港市| 道真| 道孚县| 阿克陶县| 辽阳市| 缙云县| 永新县| 九江市|