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