1. 程式人生 > 其它 >LINQ基礎篇(下)

LINQ基礎篇(下)

技術標籤:C# 基礎知識LINQtakeskipConcact分頁

分頁 Skip Take

var query = DataSource.Skip((PageNo-1)*PageSize).Take(PageSize).ToList();

Skip 跳過多少個元素

Take取出多少個元素

SkipWhile 逐一判斷直到不滿足條件,將該元素及其後的元素取出

TakeWhile 逐一判斷取出滿足條件的元素

Except

語法 a.Except(b)

作用 a中去除b中元素後剩餘部分

	    List<string> strListA = new List<string
> { "Hello","World","C#","JavaScript","Animal"}; List<string> strListB = new List<string> { "Hello", "Person", "History", "Furion", "Animal" }; var exceptValue =
strListA.Except(strListB); foreach (var item in exceptValue) { Console.WriteLine(item); } Console.ReadKey();

在這裡插入圖片描述

Concat

語法 a.Concat(b)

作用 合併a和b中的元素,不去除重複

            List<string> strListA = new List<string> { "Hello"
,"World","C#","JavaScript","Animal"}; List<string> strListB = new List<string> { "Hello", "Person", "History", "Furion", "Animal" }; var concatValue = strListA.Concat(strListB); foreach (var item in concatValue) { Console.WriteLine(item); } Console.ReadKey();

在這裡插入圖片描述

Union

語法 a.Union(b)

作用 合併a和b中的元素,去除重複

            List<string> strListA = new List<string> { "Hello","World","C#","JavaScript","Animal"};
            List<string> strListB = new List<string> { "Hello", "Person", "History", "Furion", "Animal" };
            var unionValue = strListA.Concat(strListB);
            foreach (var item in unionValue)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();

在這裡插入圖片描述

Intersect

語法 a.Intersect(b)

作用 求a和b的交集

            List<string> strListA = new List<string> { "Hello","World","C#","JavaScript","Animal"};
            List<string> strListB = new List<string> { "Hello", "Person", "History", "Furion", "Animal" };
            var intersectValue = strListA.Intersect(strListB);
            foreach (var item in intersectValue)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();

在這裡插入圖片描述

Contains

語法 a.Contains(b)

作用 類似 like ‘%b%’

		   List<string> strListA = new List<string> { "Hello","World","C#","JavaScript","Animal"};
            List<string> strListB = new List<string> { "Hello", "Person", "History", "Furion", "Animal" };
            var containsValue = from s in strListA where s.Contains("l") select s;
            foreach (var item in containsValue)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();

在這裡插入圖片描述