"EF Learning Note 11"----------Common extension methods in queries

Source: Internet
Author: User

First look at our table structure:

First of all, there's no doubt about creating our context object:

using (varnew  entities ()) {    // perform action }

Average Average:

// average of Queries Console.WriteLine (" mean score:"+db. Student.average (V=>v.score));

Max Maximum value:

// Query Highest score Console.WriteLine (" highest score:"+db. Student.max (V=>v.score));

Min min Value:

// Query Minimum score Console.WriteLine (" lowest score:"+db. Student.min (V=>v.score));

Sum sum:

// Query Total Console.WriteLine (" total score:"+db. Student.sum (V=>v.score));

Count Number:

// Query the number of students Console.WriteLine (" number:"+db. Student.count ());

Execution Result:

Contains contains:

// Query the student whose name contains the small print Console.WriteLine ("===== Print the name contains small student ====="); var query = db. Student.where (v = v.studentname.contains (" small ")); foreach (var in query) {    Console.WriteLine (St. Studentname);}

Distinct to Weight:

// to repeat Console.WriteLine ("===== prints all student scores to ====="); var query2 = db. Student.select (v=>v.score). Distinct (); foreach (var in query2) {    Console.WriteLine (ST);}

Execution Result:

Take to remove the specified number

// query Top 3 records Console.WriteLine ("===== the previous records =========="); var query3 = db. Student.take (3); foreach (var in query3) {    Console.WriteLine (St. StudentID+""+St. Studentname);}

Skip skips a specified number

// Skip Top 3 Records Console.WriteLine ("====== skips previous n records ======="); var query4 = db. Student.orderby (V=>v.studentid). Skip (3// Call the Skip method before you must use the order before the error.  foreach (var in query4) {    "" + St. Studentname);}

Use Skip and take together

// Query 3-6 Records Console.WriteLine ("===== query 2-4 records =========="); var query5 = db. Student.orderby (V=>v.studentid). Take (4). Skip (1); // Take out 4 records first--and then exclude 1 Total 3 foreach (var in query5) {    "" + St. Studentname);}

Reversed order with different results:

// Query 3-6 Records Console.WriteLine ("===== query 2-5 records =========="); var query6 = db. Student.orderby (v = v.studentid). Skip (1). Take (4); // exclude 1--and then remove 4 records total 4 foreach (var in query6) {    "" + St. Studentname);}

Description: Skip (). Take () is the first to exclude, then fetch the data. Take (). Skip () is the first to take the data and then exclude them, they return the number of different.

"EF Learning Note 11"----------Common extension methods in queries

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.