linq中的限定操作符
限定操作符運(yùn)算返回一個Boolean值,該值指示序列中是否有一些元素滿足條件或者是否所有元素都滿足條件。
一、All操作符
All方法用來確定是否序列中的所有元素都滿足條件??聪旅娴睦樱?/p>
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LimitOperation { class Program { static void Main(string[] args) { string[] source1 = new string[] { "A", "B", "C", "D", "E", "F" }; string[] source2 = new string[] { "A", "A", "A", "A", "A", "A" }; Console.WriteLine(source1.All(w => w == "A")); //輸出"False" Console.WriteLine(source2.All(w => w == "A")); //輸出 "True" Console.ReadKey(); } } }
結(jié)果:
二、Any操作符
先來看看Any的定義:
從定義中可以看出:Any有兩個重載方法。Any方法的無參方式用來確定序列是否包含任何元素。Any方法的有參方式用來確定序列中是否有元素滿足條件。只要有一個元素符合指定條件即返回true,如果一個符合指定條件的元素都沒有則返回false。看下面的例子:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LimitOperation { class Program { static void Main(string[] args) { string[] source1 = new string[] { "A", "B", "C", "D", "E", "F" }; string[] source2 = new string[] { "A", "A", "A", "A", "A", "A" }; Console.WriteLine(source1.Any()); // 輸出"True" Console.WriteLine(source1.Any(w => w == "A")); //輸出 "True" Console.WriteLine(source2.Any(w => w == "G")); //輸出 "False" Console.ReadKey(); } } }
結(jié)果:
三、Contains操作符
Contains方法用來確定序列是否包含滿足指定條件的元素。如果有返回true,否則返回false??聪旅娴睦樱?/p>
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LimitOperation { class Program { static void Main(string[] args) { string[] source1 = new string[] { "A", "B", "C", "D", "E", "F" }; Console.WriteLine(source1.Contains("A")); //輸出 "True" Console.WriteLine(source1.Contains("G")); //輸出 "False" Console.ReadKey(); } } }
結(jié)果:
Contains還有另外一個重載的方法,看定義:
public static bool Contains<TSource>(this IEnumerable<TSource> source, TSource value, IEqualityComparer<TSource> comparer);
該重載方法的參數(shù)是一個實(shí)現(xiàn)IEqualityComparer<TSource>接口的類型??聪旅娴睦?。
定義實(shí)現(xiàn)IEqualityComparer<TSource>接口的類型:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LimitOperation { /// <summary> /// EqualityComparerEquals類實(shí)現(xiàn)IEqualityComparer接口 /// </summary> public class EqualityComparerEquals : IEqualityComparer<string> { public bool Equals(string x, string y) { return x == y; } public int GetHashCode(string obj) { return obj.ToString().GetHashCode(); } } }
方法中調(diào)用:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LimitOperation { class Program { static void Main(string[] args) { string[] source1 = new string[] { "A", "B", "C", "D", "E", "F" }; var comparer = source1.Contains("F", new EqualityComparerEquals()); Console.WriteLine(comparer); //輸出"True" Console.ReadKey(); } } }
結(jié)果:
注意:在自定義的類中,x相當(dāng)于數(shù)組中的每一個元素,y是要比較的元素:F。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
讓VS2008對JQuery語法的智能感知更完美一點(diǎn)
上周Rich Strahl的POST中提到一個新發(fā)布的VS2008 hotfix修復(fù)了VS2008對Javascript智能感知的一些BUG,fixed之后可以讓VS2008對JQuery提供智能感知,讓我等JQuery fans欣喜了一陣子。可仔細(xì)一看,還需要另外給JQuery添加上XML comments才行 :( 喪氣了...2008-03-03Visual Studio 2015 配置 Opencv3.2的圖文詳解
這篇文章主要介紹了Visual Studio 2015 配置 Opencv3.2的圖文詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05Asp.net?mvc實(shí)現(xiàn)上傳頭像加剪裁功能
這篇文章主要介紹了實(shí)現(xiàn)Asp.net?mvc上傳頭像加剪裁功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2015-08-08asp.net TextBox控件設(shè)置ReadOnly后,不能回傳。
當(dāng)把一個TextBox控件ReadOnly屬性設(shè)置為True后,這個控件就不回傳了。2009-05-05ASP.NET檢測到不安全 Request.Form 值解決方案匯總
這篇文章主要介紹了ASP.NET檢測到不安全 Request.Form 值解決方案匯總 ,十分的全面,需要的朋友可以參考下2015-06-06asp.net訪問Access數(shù)據(jù)庫溢出錯誤
asp.net訪問Access數(shù)據(jù)庫溢出錯誤,大家會的幫忙解決下啊。2009-07-07