We use a database of hotel management systems as an example
Table structure is simple: guestinfo (guest information table), Room (rooms table), Roomtype (room type table), Hotelinfo (monthly earnings information)
As a whole, there is a foreign key to the room type ID, and a foreign key for the ID of the room in the guest table. Monthly earnings information regardless of first
Preparation: New Project LINQ_CH4 console program, new DB folder
Right-click the DB folder, add → new item
Select the LINQ to SQL class, file name hotel.dbml, add
Open Server Explorer, my shortcut key is Ctrl+w+l
Select the four tables, drag and drop them into the hotel.dbml file that is already open, and then ctrl+s save
1.1 Query Single Table
Introduce namespaces so that we can access the dbml file
We have an object called DBML filename +datacontext a name object that you can manipulate the database through it
① found empty room, can also live more than two people in the room information; The results are displayed in descending order of maximum number of inhabitants. Results in descending order of number of beds (exercise basic single-table query, multi-criteria sorting)
The code is as follows:
Hoteldatacontext db =NewHoteldatacontext ();//here is the front class name +datacontext//1. Find the empty room, you can also live more than two people in the room information; The results are displayed in descending order of maximum number of inhabitants, and then in descending order of the number of beds. varQuery1 = fromOinchdb. thewhereO.r_status = =0&& o.r_maxpnum >1 byo.r_maxpnum Descending, O.r_bednum descendingSelecto; foreach(varIteminchQuery1) {Console.WriteLine (string. Format ("number of beds in room id:{0}\t{1}\t: {2}, up to {3}, Description: {4}", item. R_ID, item. R_status = =0?"Idle":"someone", item. R_bednum, item. R_maxpnum, item. r_discription)); }
:
Getting Started with LINQ to SQL LINQ