LINQ: aggregate; sequenceequal; join

Source: Internet
Author: User

1. Aggregate (use aggregate to create the concatenation of arrays and calculate the total product of all elements .) :

Double [] doubles = {1.7, 2.3, 1.9, 4.1, 2.9 };

Double Product = doubles. Aggregate (runningproduct, nextfactor) => runningproduct * nextfactor );

2. Aggregate heavy load (use aggregate to create a sequential account balance, from the initial balance of 100 minus the amount retrieved each time until the balance is reduced to less than 0 .) :

Double startbalancing = 100.0;

Int [] attemptedwithdrawals = {20, 10, 40, 50, 10, 70, 30 };

Double endbalance =
Attemptedwithdrawals. Aggregate (startbalance,
(Balance, nextwithdrawal) =>
(Nextwithdrawal <= balance )? (Balance-nextwithdrawal): Balance ));

3. sequenceequal (use sequenceequals to check whether all elements in the two sequences match in the same order .) :

VaR wordsa = new string [] {"Cherry", "apple", "blueberry "};
VaR wordsb = new string [] {"Cherry", "apple", "blueberry "};

Bool match = wordsa. sequenceequal (wordsb );

4. sequenceequal overload (custom comparison method ):

List <student> List = new list <student> ();

List <student> list2 = new list <student> ();

Student A = new student
{
Userid = 1,
Studentname = "Eric"

};
Student B = new student
{
Userid = 1,
Studentname = "Eric"

};

Student C = new student
{
Userid = 2,
Studentname = "laoyi"

};
List. Add ();
List. Add (B );

List. Add (C );

List2.add (C );
List2.add (B );

List2.add ();

VaR TT = List. sequenceequal (list, new studentcomparer ());
 

Public class student
{
Public int userid {Get; set ;}
Public String studentname {Get; set ;}
}
 

Custom comparison class:
Public class studentcomparer: iequalitycomparer <student>
{
Public bool equals (student X, student y)
{
Return X. userid. Equals (Y. userid );
}

Public int gethashcode (student OBJ)
{
Return obj. userid. gethashcode ();
}
}

5. Join in (left Outer Join and composite key, multiple key values are encapsulated using an anonymous type ):

List <customer> MERs = getcustomerlist ();
List <supplier> suppliers = getsupplierlist ();

VaR suppliercusts =
From sup in suppliers
Join Cust in customers on new {sup. City, sup. Country} equals New {Cust. City, Cust. Country} into CS
From C in CS. defaultifempty () // call the defaultifempty method to make it an internal link
Orderby sup. suppliername
Select New {Country = sup. Country,
City = sup. City,
Suppliername = sup. suppliername,
CompanyName = c = NULL? "(No customers)": C. companyName
};

 

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.