PROWAREtech

articles » current » dot-net » tutorial » linq » page-3

.NET: Count Operator, Where and OrderByDescending - Language Integrated Query (LINQ)

Language Integrated Query (LINQ) Tutorial - Page 3.

The Count() Operator

Count() returns the record count.

using System;
using System.Collections.Generic;
using System.Linq;

namespace LinqExample
{
	[Serializable]
	public class Person : IComparable<Person>, IFormattable
	{
		public string LastName { get; set; }
		public string FirstName { get; set; }
		public int Age { get; set; }
		public string[] Cars { get; private set; }

		public Person(string LastName = null, string FirstName = null, int Age = 0, IEnumerable<string> Cars = null)
		{
			this.LastName = LastName;
			this.FirstName = FirstName;
			this.Age = Age;
			List<string> cars = new List<string>();
			foreach (string car in Cars)
			{
				cars.Add(car);
			}
			this.Cars = cars.ToArray();
		}
		public int CompareTo(Person person)
		{
			if (person == null)
				throw new ArgumentNullException("person");
			return LastName.CompareTo(person.LastName);
		}
		public override string ToString()
		{
			return LastName + ", " + FirstName;
		}
		public string ToString(string format)
		{
			return ToString(format, null);
		}
		public string ToString(string format, IFormatProvider FormatProvider)
		{
			switch (format)
			{
				case null:
				case "N": // return full name
					return ToString();
				case "L": // return last name
					return LastName;
				case "F": // return first name
					return FirstName;
				case "A": // return all information
					return ToString() + " - " + Age.ToString();
				default:
					throw new FormatException("Format " + format + " unsupported");
			}
		}
	}

	public static class Classroom
	{
		private static List<Person> students;

		public static IList<Person> GetAttendance()
		{
			if (students == null)
			{
				students = new List<Person>();
				students.Add(new Person("Holman", "Kaylee", 35, new string[] { "Civic", "Smart Car" }));
				students.Add(new Person("Crabtree", "Glenn", 30, new string[] { "Element" }));
				students.Add(new Person("Kellerman", "Calandra", 30, new string[] { "F150", "Civic" }));
				students.Add(new Person("Chairez", "Judy", 27, new string[] { "Outback", "Element" }));
				students.Add(new Person("Santore", "Cari", 42, new string[] { "F250" }));
				students.Add(new Person("Barnaby", "Emelda", 27, new string[] { "Cayenne" }));
				students.Add(new Person("Beeler", "Rufina", 22, new string[] { "320i", "911" }));
				students.Add(new Person("Barreiro", "Jessia", 31, new string[] { "WRX", "Civic", "Ridgeline" }));
				students.Add(new Person("Kummer", "Bebe", 30, new string[] { "F150" }));
				students.Add(new Person("Sexson", "Aleida", 25, new string[] { "Outback" }));
				students.Add(new Person("Ansell", "Freida", 44, new string[] { "Element" }));
				students.Add(new Person("Eriksson", "Jae", 49, new string[] { "Pilot", "Accord" }));
			}
			return students;
		}
	}

	class Program
	{
		static void Main()
		{
			var countQuery = from p in Classroom.GetAttendance()
						where p.Cars.Count() >= 2
						orderby p.Cars.Count() descending
						select p;
			foreach (Person obj in countQuery)
			{
				Console.WriteLine("{0:A}; cars: {1}", obj, obj.Cars.Count());
			}
		}
	}
}
Barreiro, Jessia - 31; cars: 3
Holman, Kaylee - 35; cars: 2
Kellerman, Calandra - 30; cars: 2
Chairez, Judy - 27; cars: 2
Beeler, Rufina - 22; cars: 2
Eriksson, Jae - 49; cars: 2
Using Lambda with LINQ Methods Where() and OrderByDescending()
using System;
using System.Collections.Generic;
using System.Linq;

namespace LinqExample
{
	[Serializable]
	public class Person : IComparable<Person>, IFormattable
	{
		public string LastName { get; set; }
		public string FirstName { get; set; }
		public int Age { get; set; }
		public string[] Cars { get; private set; }

		public Person(string LastName = null, string FirstName = null, int Age = 0, IEnumerable<string> Cars = null)
		{
			this.LastName = LastName;
			this.FirstName = FirstName;
			this.Age = Age;
			List<string> cars = new List<string>();
			foreach (string car in Cars)
			{
				cars.Add(car);
			}
			this.Cars = cars.ToArray();
		}
		public int CompareTo(Person person)
		{
			if (person == null)
				throw new ArgumentNullException("person");
			return LastName.CompareTo(person.LastName);
		}
		public override string ToString()
		{
			return LastName + ", " + FirstName;
		}
		public string ToString(string format)
		{
			return ToString(format, null);
		}
		public string ToString(string format, IFormatProvider FormatProvider)
		{
			switch (format)
			{
				case null:
				case "N": // return full name
					return ToString();
				case "L": // return last name
					return LastName;
				case "F": // return first name
					return FirstName;
				case "A": // return all information
					return ToString() + " - " + Age.ToString();
				default:
					throw new FormatException("Format " + format + " unsupported");
			}
		}
	}

	public static class Classroom
	{
		private static List<Person> students;

		public static IList<Person> GetAttendance()
		{
			if (students == null)
			{
				students = new List<Person>();
				students.Add(new Person("Holman", "Kaylee", 35, new string[] { "Civic", "Smart Car" }));
				students.Add(new Person("Crabtree", "Glenn", 30, new string[] { "Element" }));
				students.Add(new Person("Kellerman", "Calandra", 30, new string[] { "F150", "Civic" }));
				students.Add(new Person("Chairez", "Judy", 27, new string[] { "Outback", "Element" }));
				students.Add(new Person("Santore", "Cari", 42, new string[] { "F250" }));
				students.Add(new Person("Barnaby", "Emelda", 27, new string[] { "Cayenne" }));
				students.Add(new Person("Beeler", "Rufina", 22, new string[] { "320i", "911" }));
				students.Add(new Person("Barreiro", "Jessia", 31, new string[] { "WRX", "Civic", "Ridgeline" }));
				students.Add(new Person("Kummer", "Bebe", 30, new string[] { "F150" }));
				students.Add(new Person("Sexson", "Aleida", 25, new string[] { "Outback" }));
				students.Add(new Person("Ansell", "Freida", 44, new string[] { "Element" }));
				students.Add(new Person("Eriksson", "Jae", 49, new string[] { "Pilot", "Accord" }));
			}
			return students;
		}
	}

	class Program
	{
		static void Main()
		{
			var countQuery = Classroom.GetAttendance().Where(p => p.Cars.Count() >= 2).OrderByDescending(p => p.Cars.Count());
			foreach (Person obj in countQuery)
			{
				Console.WriteLine("{0:A}; cars: {1}", obj, obj.Cars.Count());
			}
		}
	}
}
Barreiro, Jessia - 31; cars: 3
Holman, Kaylee - 35; cars: 2
Kellerman, Calandra - 30; cars: 2
Chairez, Judy - 27; cars: 2
Beeler, Rufina - 22; cars: 2
Eriksson, Jae - 49; cars: 2
<<<[Page 3 of 7]>>>

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
CLOSE