Simple Count based on a Condition using LINQ

Here’ a simple example demonstrating how to perform a count on a List<> based on a condition


class Program
static void Main(string[] args)
List<Students> students = new List<Students>();
students.Add(new Students() { StudentId = 1, Marks = 8.0f });
students.Add(new Students() { StudentId = 2, Marks = 5.0f });
students.Add(new Students() { StudentId = 3, Marks = 7.0f });
students.Add(new Students() { StudentId = 4, Marks = 9.5f });
students.Add(new Students() { StudentId = 1, Marks = 9.0f });

var topStudCount = students.Count(r => r.Marks >= 9.0f);
Console.WriteLine("Students with Marks >=9 are {0}", topStudCount);

class Students
public int StudentId { get; set; }
public float Marks { get; set; }


Use this Converter Tool


LINQ Count

Will you give this article a +1 ? Thanks in advance

About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

No comments: