In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to use the qualifying operator in linq". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The qualifying operator operation returns a Boolean value indicating whether some elements in the sequence satisfy the condition or whether all elements satisfy the condition.
I. All operator
The All method is used to determine whether all elements in the sequence satisfy the condition. Look at the following example:
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"}; Console.WriteLine (source1.All (w = > w = = "A")); / / output "False" Console.WriteLine (source2.All (w = > w = = "A")); / / output "True" Console.ReadKey () }}}
Results:
II. Any operator
Let's take a look at the definition of Any:
You can see from the definition that Any has two overloaded methods. The no-parameter method of the Any method is used to determine whether the sequence contains any elements. The parametric method of the Any method is used to determine whether any element in the sequence satisfies the condition. True is returned as long as one element meets the specified criteria, and false is returned if none of the elements meet the specified criteria. Look at the following example:
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"}; Console.WriteLine (source1.Any ()); / / output "True" Console.WriteLine (source1.Any (w = > w = = "A"); / / output "True" Console.WriteLine (source2.Any (w = > w = = "G")) / / output "False" Console.ReadKey ();}
Results:
3. Contains operator
The Contains method is used to determine whether the sequence contains elements that meet the specified criteria. If true is returned, false is returned otherwise. Look at the following example:
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")) / output "True" Console.WriteLine (source1.Contains ("G")); / / output "False" Console.ReadKey ();}
Results:
Contains also has another way to overload, see the definition:
Public static bool Contains (this IEnumerable source, TSource value, IEqualityComparer comparer)
The parameter to this overloaded method is a type that implements the IEqualityComparer interface. Look at the following example.
Define the type that implements the IEqualityComparer interface:
The using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace LimitOperation {/ EqualityComparerEquals class implements the IEqualityComparer interface / public class EqualityComparerEquals: IEqualityComparer {public bool Equals (string x, string y) {return x = = y;} public int GetHashCode (string obj) {return obj.ToString () .GetHashCode () }}}
Method to call:
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); / / output "True" Console.ReadKey ();}
Results:
Note: in a custom class, x is equivalent to every element in the array, and y is the element to be compared: F.
This is the end of the content of "how to use qualification operators in linq". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.