Getting Started with LINQ to SQL LINQ

Source: Internet
Author: User

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

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.