All

In this chapter you will learn:

  1. How to use All operator
  2. All with condition
  3. 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:

  1. How to use Any operator
  2. Any without expression
  3. Any with mod operator
  4. Any with string operator
  5. How to use Any operator with custom types
Home » C# Tutorial » Linq Operators
Aggregate
Aggregate with seed
Aggregate string value
All
Any
Average
Cast
Concat
Contains
Count
DefaultIfEmpty
Distinct
ElementAt
ElementAtOrDefault
Empty
Except
FindAll
First
FirstOrDefault
GroupBy
Intersect
Last
LastOrDefault
LongCount
Max
Min
OfType
OrderBy
OrderByDescending
Range
Repeat
Reverse
SelectMany
SequenceEqual
Single
SingleOrDefault
Skip
SkipWhile
Sum
Take
TakeWhile
ThenBy
ThenByDescending
ToArray
ToList
Zip