LongCount

In this chapter you will learn:

  1. How to use LongCount operator
  2. How to use LongCount with filter delegate

Get to know LongCount operator

LongCount returns an long type value that represents the total number of elements in a sequence.

using System;/*  j  a  v  a2 s.co  m*/
using System.Linq;
using System.Collections;
using System.Collections.Generic;

public class MainClass {
    public static void Main() {
        string[] fruits = { "apple", "banana", "mango", 
                    "orange", "passionfruit", "java2s.com" };
        
        long count = fruits.LongCount();
        Console.WriteLine("There are {0} fruits in the collection.", count);
    }
}

LongCount filter delegate

using System;/*from   j a v a 2s .  co m*/
using System.Linq;
public class MainClass {
    public static void Main() {
        int[] intArray= { 1,2,3,4,5,6,7,8,9};
        
        long count = fruits.LongCount(n => n > 1 && n < 4);
        Console.WriteLine(count);
    }
}

Next chapter...

What you will learn in the next chapter:

  1. How to use Max operator
  2. Max with filter delegate
  3. Max among string array
  4. Max for custom types
  5. How to get the longest word
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