Learn LINQ to SQL

Source: Internet
Author: User

Linq to Sql is a very magical thing

He can keep you focused on the business logic and use the same statement to manipulate multiple relational databases

Let's take a look at his usage .....

Database fields

 UseMasterGoCreate DatabaseLearn_linq_to_sqlGo UseLearn_linq_to_sqlGoCreate TableClass (CIdint Primary Key  Identity(1,1), CNamenvarchar( -) not NULL)GoCreate TableStudent (Idint Primary Key Identity(1,1), Namenvarchar(Ten) not NULL, Shuxueint, Yuwenint, CIdint Foreign Key ReferencesClass (CId))GoInsert  intoClassValues('122'),('123'),('124'),(' the')GoInsert  intoStudentValues('Small white', the, +,1),('Side Dishes', the, the,2),('Xiao Fang', +, the,4),('Xiao Li', $, -,3),('Xiao Zhang', About, the,3),('Small Day', $, the,3),('Xiao Yang', +, the,2),('Xiao Qing', the,Bayi,1),('Xiao Huang', the, About,4),('Xiao Wu',98, -,1)

  With the database we need to create a LINQ to SQL in the project

The generated entity navigation properties are as follows:

You can see that the compiler automatically helps us produce the class

And then we can try to use the

  

implement query ....

In addition, some of the CLR's features can be used

            #regionLINQ connections//var clas = DC.            Gettable<class> (); //var student = DC.            Gettable<student> (); //var result = from Cl in Clas//join S in student on CL. CId equals s.cid into temp//From the T in temp. DefaultIfEmpty ()            //Select New {cname=cl.            Cname,name=t.name}; //foreach (var item in result)//{            //Console.WriteLine ("Class:" +item.            CName); //Console.WriteLine (item.            Name); //}            #endregion            #regionRelationship//var result = DC1.            Gettable<class> (); //foreach (Class cl in result)//{            //Console.WriteLine ("Class:" + CL.)            CName); //foreach (Student item in CL. Student)//    {            //Console.WriteLine ("\ T" + item.)            Name); //    }            //}            #endregion            #regionSetting Load Parameters// dataloadoptions ds = new DataLoadOptions (); //ds. Loadwith<class> (d =>d.student);//specifies that the load//DC1.            Loadoptions = ds; //var result = DC1.            Gettable<class> (); //foreach (Class cl in result)//{            //Console.WriteLine ("Class:" + CL.)            CName); //foreach (Student item in CL. Student)//    {            //Console.WriteLine ("\ T" + item.)            Name); //    }            //}            #endregion            #regionModify//var result = DC1.            Gettable<student> (); //foreach (var item in result)//{            //item.            Yuwen-= 5; //}            //DC1. SubmitChanges ();            #endregion            #regionDelete and add//var students = DC1. Gettable<student> ();            ////student s = new Student (); ////s.yuwen = 88; ////s.shuxue = 79; ////s.name = "I am SB"; ////s.cid = 2; ////students.            InsertOnSubmit (s); ////DC1. SubmitChanges ();            //students. DeleteOnSubmit (Students. Where (d = d.name = = "I am SB").            FirstOrDefault ()); //DC1. SubmitChanges ();            #endregion

Learn LINQ to SQL

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.