EF Learning Note 05----------dbcontext Basic Query

Source: Internet
Author: User

Traverse all Entities
// Traverse all students DBSet using (varnew  entities ()) {    foreach (var in db). Student)    {        objectdumper.write (Student. studentname);    }};

Description: Print the names of all students

Query by condition
using(vardb =Newentities ()) {    //Find all students born after January 1, 1991     var  day = Convert.todatetime ("1991-1-1"); varStudents = fromVinchDb. StudentwhereV.birthday >=   DaySelectv; foreach(varStudentinchstudents) {Objectdumper.write (student). Studentname+"-"+student.    Birthday); }};

Execution Result:

Note here: When using LINQ and Lamba , variables must be dumped in advance, and C # functions cannot be used inside LINQ.

The wrong wording:

using(vardb =Newentities ()) {    varStudents = fromVinchDb. StudentwhereV.birthday >=convert.todatetime ("1991-1-1") Selectv; foreach(varStudentinchstudents) {Objectdumper.write (student). Studentname+"-"+student.    Birthday); }};

This writes directly to the error:

Sort in ascending order by birthday (default)
using (varnew  entities ()) {    var from in db. Student                      v.birthday                    Select  v    ; foreach (var in students)    {        objectdumper.write (student);    }};

Descending:descending

var  from inch db. Student                Descending                select v;
Querying a single Entity find method
// find queries a single entity using (varnew  entities ()) {    var student = db. Student.find (1);    Objectdumper.write (student);};

Description: Queries student information based on primary key and returns NULL if not found

Querying individual Entities single method
// single query for individual entities using (varnew  entities ()) {    var from in db. Student                    where v.studentname==" Zhang San "                    Select v;     // Single method can not find records or multiple records will be error     objectdumper.write (student. single ());};

Note: Calling the single method can not find records or multiple records will be error

Querying a single Entity Singleordefault method
 // singleordefault queries a single entity  using  (var  db = new   entities ()) { var  student = from  V in   db. Student  where  v.studentname = = "   John Doe  "  select   V;  // singleordefault method does not find records that return null. If a query to multiple records results in an error   Objectdumper.write (student. Singleordefault ());};  

Description: Calling the Singleordefault method does not find the record put back null if the query to multiple records error

Full case:

//Traverse All Students            using(vardb =Newentities ()) {                varStudents = fromVinchDb. StudentSelectv; foreach(varStudentinchstudents)                {objectdumper.write (student);            }            }; Console.WriteLine ("========== by birthday sort ==========="); //Sort by Birthday ascending            using(vardb =Newentities ()) {                varStudents = fromVinchdb. Student byV.birthday DescendingSelectv; foreach(varStudentinchstudents)                {objectdumper.write (student);            }            }; Console.WriteLine ("==========find querying a single entity ==========="); //find queries a single entity            using(vardb =Newentities ()) {                varStudent = db. Student.find (1);            Objectdumper.write (student);            }; Console.WriteLine ("==========single querying a single entity ==========="); //single query for individual entities            using(vardb =Newentities ()) {                varStudent = fromVinchdb. Studentwherev.studentname=="Zhang San"                                Selectv; //Single method can not find records or multiple records will be errorObjectdumper.write (student.            single ());            }; Console.WriteLine ("==========singleordefault querying a single entity ==========="); //Singleordefault querying a single entity            using(vardb =Newentities ()) {                varStudent = fromVinchdb. StudentwhereV.studentname = ="John Doe"                                Selectv; //Singleordefault method does not find records return NULL, if query to multiple records will be errorObjectdumper.write (student.            Singleordefault ()); };

EF Learning Note 05----------dbcontext Basic Query

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.