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