Standard operators and methods used by LINQ

Source: Internet
Author: User

# Region LINQ standard query operator (LAMBDA Method) Note: C # overload with Integer Parameters (indexes) in standard query operators is not supported

// 1. Filter Method for standard query operators -- where
// Iqueryable <student> stu1 = dB. Student. Where (S => S. ssex = "male ");
// Gridview1.datasource = stu1;
// Gridview1.databind ();


// 2. Select of the standard query operator

// Var stu2 = dB. Student. Select (S => New {S. Sno, S. sname, S. Sage}); // This is called an anonymous object
// Gridview2.datasource = stu2;
// Gridview2.databind ();


// 3. sorting method of standard query operators -- order
// Sort in ascending order
VaR stu3 = dB. Student. orderby (S => S. Sage );
Gridview1.datasource = stu3;
Gridview1.databind ();

// Sort by multiple criteria in descending order
VaR stu4 = dB. Student. orderbydescending (S => S. Sage). thenbydescending (S => S. SnO );
Gridview2.datasource = stu4;
Gridview2.databind ();


// 4. Join set of standard operators -- join
VaR stu5 = dB. student. join (dB. stucourse, S => S. sno, Stu => Stu. sno, (S, Stu) => New {SnO = S. sno, scsno = Stu. sno, S. sname, Stu. CID });
Gridview2.datasource = stu5;
Gridview2.databind ();


// Left join
VaR leftjoin = from Stu in db. Student
Join SC in db. stucourse
On Stu. Sno equals SC. SnO
Into temp
From t in temp. defaultifempty ()
Select New {Stu. Sno, Stu. Sage, cid = T = NULL? 0: T. CID };

// Right join
VaR right = from SC in db. stucourse
Join Stu in db. Student
On SC. Sno equals Stu. SnO
Into temp
From TT in temp. defaultifempty ()
Select New {SC. CID, SC. Sno, sname = TT = NULL? "": TT. sname };

// 5. Group of standard operators-groupby
// Group by age
VaR stu6 = dB. Student. groupby <student, int> (S => (INT) S. Sage );
List <igrouping <int, student> list2 = stu6.tolist ();
Foreach (igrouping <int, student> item in list2)
{
// Grouping conditions of the output Group
Response. Write (string. Format ("group: {0} <br/>", item. Key ));
// Traverse all elements in the group
Foreach (student ST in item)
{
Response. Write (string. Format ("Name: {0}", st. sname ));
}
Response. Write ("<br/> ");
}

// 6. Paging data of standard operators-Skip + take
// Assume that each page has 5 Records
// Int pagesize = 2;
// Return list. Skip (pageindex-1) * pagesize). Take (pagesize). tolist ();
VaR pageone = dB. Student. Skip <student> (24). Take (6 );
Gridview1.datasource = pageone;
Gridview1.databind ();

# Endregion

 

 

Additional:

Int [] numbers = {0, 30, 15, 90, 85, 40, 75, 20 ,};
Int [] arr = {};
Int first = numbers. First <int> (M => m> 35); // return the first record 90 that meets the conditions.
Int last = numbers. Last <int> (n => n <40); // returns the last record 20 that meets the conditions.
Bool allresult = numbers. All <int> (Mm => mm <100); // determines whether all elements meet true.
Bool allresult2 = numbers. All <int> (Mm => MM> 5); // false
Bool anyresult = numbers. Any (); // return true if there is an element in numbers
Bool anyresult1 = arr. Any (); // returns false if no element in arr
Bool anyresult2 = numbers. Any <int> (Mm => mm <1); // returns true if one element meets the conditions.
Bool anyresult3 = numbers. Any <int> (Mm => MM> 89); // true
Bool anyresult5 = numbers. Any (M => m> 200); // false

Standard operators and methods used by 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.