Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the qualifying operator in linq

Shulou Source: shulou.com Published: 2022-06-01 06:31:03 09月27日 Update

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!

Tags: Elements outputs methods conditions operators sequences examples results facets interfaces contents methods more knowledge types utility learning and then two parameters Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS MySQL Huawei Redmi OPPO Reno