jQuery and ASP.NET

March 25, 2010

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

C#

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);
Console.ReadLine();
}
}

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

VB.NET

Use this Converter Tool

OUTPUT

LINQ Count



'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP 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 has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

0 Responses to "Simple Count based on a Condition using LINQ"
 

Copyright © 2009-2011 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions