All
In this chapter you will learn:
- How to use All operator
- All with condition
- Using All to determine whether an array contains only odd numbers
All operator
All determines whether all the elements of a collection satisfy a condition.
using System;// j a va 2 s . c o m
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
public class MainClass{
public static void Main(string[] args){
String[] TestData = {"Zero", "One", "Two", "Three",
"Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten"};
var ThisQuery = TestData.All(ThisElement => ThisElement.Length == 4);
Console.WriteLine("All elements have 4 characters: " + ThisQuery);
}
}
All with condition
using System;/*from ja v a 2 s. co m*/
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
public class MainClass{
public static void Main(){
int[] numbers = { 2, 6, 1, 56, 102 };
Console.Write(numbers.All(e => e % 2 == 0) ? "Yes, they are" : "No, they aren't");
}
}
All with filter
using System;// j a v a2 s . c om
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class MainClass {
public static void Main() {
int[] numbers = { 1, 11, 3, 19, 41, 65, 19 };
bool onlyOdd = numbers.All(n => n % 2 == 1);
Console.WriteLine("The list contains only odd numbers: {0}", onlyOdd);
}
}
Next chapter...
What you will learn in the next chapter:
- How to use Any operator
- Any without expression
- Any with mod operator
- Any with string operator
- How to use Any operator with custom types
Home » C# Tutorial » Linq Operators