PROWAREtech
.NET: Language Integrated Query (LINQ) - Page 5
Min, Max, Average and Sum Operators.
The Min() Max() Average() Sum() Operators
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 Person(string LastName = null, string FirstName = null, int Age = 0)
{
this.LastName = LastName;
this.FirstName = FirstName;
this.Age = Age;
}
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));
students.Add(new Person("Crabtree", "Glenn", 30));
students.Add(new Person("Kellerman", "Calandra", 30));
students.Add(new Person("Chairez", "Judy", 27));
students.Add(new Person("Santore", "Cari", 42));
students.Add(new Person("Barnaby", "Emelda", 27));
students.Add(new Person("Beeler", "Rufina", 22));
students.Add(new Person("Barreiro", "Jessia", 31));
students.Add(new Person("Kummer", "Bebe", 30));
students.Add(new Person("Sexson", "Aleida", 25));
students.Add(new Person("Ansell", "Freida", 44));
students.Add(new Person("Eriksson", "Jae", 49));
}
return students;
}
}
class Program
{
static void Main()
{
var minmaxQuery = from p in Classroom.GetAttendance()
group p by 0 into g
select new
{
Min = (from p2 in g
select p2.Age).Min(),
Max = (from p2 in g
select p2.Age).Max(),
Average = (from p2 in g
select p2.Age).Average(),
Sum = (from p2 in g
select p2.Age).Sum(),
};
foreach (var obj in minmaxQuery)
{
Console.WriteLine("Min: {0}", obj.Min);
Console.WriteLine("Max: {0}", obj.Max);
Console.WriteLine("Average: {0:0,0}", obj.Average);
Console.WriteLine("Sum: {0}", obj.Sum);
}
}
}
}
Min: 22 Max: 49 Average: 33 Sum: 392
Using Lambda with LINQ Methods GroupBy() and Select()
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 Person(string LastName = null, string FirstName = null, int Age = 0)
{
this.LastName = LastName;
this.FirstName = FirstName;
this.Age = Age;
}
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));
students.Add(new Person("Crabtree", "Glenn", 30));
students.Add(new Person("Kellerman", "Calandra", 30));
students.Add(new Person("Chairez", "Judy", 27));
students.Add(new Person("Santore", "Cari", 42));
students.Add(new Person("Barnaby", "Emelda", 27));
students.Add(new Person("Beeler", "Rufina", 22));
students.Add(new Person("Barreiro", "Jessia", 31));
students.Add(new Person("Kummer", "Bebe", 30));
students.Add(new Person("Sexson", "Aleida", 25));
students.Add(new Person("Ansell", "Freida", 44));
students.Add(new Person("Eriksson", "Jae", 49));
}
return students;
}
}
class Program
{
static void Main()
{
var minmaxQuery = Classroom.GetAttendance()
.GroupBy(g => 0)
.Select(g => new
{
Min = g.Select(p => p.Age).Min(),
Max = g.Select(p => p.Age).Max(),
Average = g.Select(p => p.Age).Average(),
Sum = g.Select(p => p.Age).Sum()
});
foreach (var obj in minmaxQuery)
{
Console.WriteLine("Min: {0}", obj.Min);
Console.WriteLine("Max: {0}", obj.Max);
Console.WriteLine("Average: {0:0,0}", obj.Average);
Console.WriteLine("Sum: {0}", obj.Sum);
}
}
}
}
Comment