The aggregation function Part-2 in LINQ

Source: Internet
Author: User

aggregate functions in LINQ are most commonly used with sum (), Average (), Max (), Min (). As the name implies, these aggregate functions are used to get "sum", "average", "maximum", "minimum" in the Recordset. The use of these functions is also very simple, as shown in the following examples.

1) Sum ()

list<intnew list<int133278  45639  }; int sumoflist = intinlist.sum (); Console.WriteLine (sumoflist);

2) Average ()

list<intnew list<int133278  45639  }; double averageoflist = intinlist.average (); Console.WriteLine (averageoflist);

3) Max ()

list<intnew list<int133278  45639int maxoflist =  Intinlist.max (); Console.WriteLine (maxoflist);

4) Min ()

list<intnew list<int133278  45639  }; int minoflist = intinlist.min (); Console.WriteLine (minoflist);

The usage of commonly used aggregate functions is described above, and note that I use a list of type int, if we have a custom class, how to use it? To be more precise, for example, now I have a person class with FirstName and LastName properties, so what happens when I call the min () function? As shown in the following code:

 Public classperson{ Public stringFirstName;  Public stringLastName;}classprogram{Static voidMain (string[] args) {List<Person> personinlist =NewList<person>()         {            NewPerson () {FirstName="Jonh", LastName="Liu"            },            NewPerson () {FirstName="Sara", LastName="Guo"            },            NewPerson () {FirstName="Marttin", LastName="Zeng"            },            NewPerson () {FirstName="Sccot", LastName="Jiang"            }        }; Person Minperson=personinlist.min ();    Console.WriteLine (Minperson.firstname); }}

In fact, the above code will have a run-time exception:

Object must implement IComparable.   At System.Collections.Comparer.Compare (object A, object B) at   System.linq.enumerable.min[tsource] (IEnumerable '  1  source)   inwuyicontinue ...

The reason is. NET doesn't know how to judge the person class to get its minimum value. List<int> is successful because. NET knows how to compare int data to find the minimum value. In order to let. NET knows how to compare two person to determine their size we need to have the person class implement an interface called Icomaparable<t>, which defines how to determine the size of two objects. We make the following changes to the person class:

 Public class Person:icomparable<person>{    publicstring  FirstName;      Public string LastName;      Public int CompareTo (person obj)    {        returnthis. Firstname.compareto (obj. FirstName);}    }

The code above enables the person class to be compared with the FirstName property, and the following code finds the Personinlist object that corresponds to the smallest FirstName in the list:

Person Minperson = personinlist.min (); Console.WriteLine (minperson.firstname);

Of course, if we do not want to modify the person class, because the person class is probably a third-party already written class, we can not modify an already packaged DLL file, then we also call Min () of the overloaded function:

 Public Static TResult Min<tsource, tresult> ( This ienumerable<tsource> source, Func<tsource, tresult> selector);

Here, you can pass in a proxy to Min (), which specifies how to sort the person, as in the following code:

string minfirstname = personinlist.min (p = p.firstname); Console.WriteLine (minfirstname);

The aggregation function Part-2 in LINQ

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.