LINQ to Sql系列二 简单查询和联接查询
LINQ to Sql系列二 简单查询和联接查询 这一篇文章主要总结LINQ to sql的简单查询(单表查询)和联接查询(多表查询) 单表查询 需求是我们要输出TClass表中的结果。使用了from…in…select语句,代码如下: SimpleQuery(){ using (L2SDBDataContext db = new L2
LINQ to Sql系列二 简单查询和联接查询
这一篇文章主要总结LINQ to sql的简单查询(单表查询)和联接查询(多表查询)
单表查询需求是我们要输出TClass表中的结果。使用了from…in…select语句,代码如下:
SimpleQuery() { using (L2SDBDataContext db = new L2SDBDataContext()) { var query = from tc in db.TClasses { ClassID=tc.ClassID, ClassName=tc.ClassName }; Console.WriteLine(); int i = 1; foreach (var item in query) { Console.WriteLine(,i,item.ClassID,item.ClassName); i++; } } }
注意:这里没有用select tc,而是定义了一个新的匿名类型是因为性能的考虑,后面性能优化的部分会讲到。
输出结果:
多表查询,也可以称为联接查询,香港服务器,需要通过外键联接多张表才能查询到我们想要的结果,现在的需求是某个班级及这个班级相关学生的信息。实现这个需求有两种方法,网站空间,一种是内联接,另一种方法是外联接,下面是通过内联接查询的代码。
Query_InnerJoin() { using (L2SDBDataContext db = new L2SDBDataContext()) { var query = from s in db.TStudents join c in db.TClasses on s.ClassID equals c.ClassID { ClassID = s.ClassID, ClassName = c.ClassName, Student = new { Name = s.Name, StudentID = s.StudentID } }; foreach (var item in query) { Console.WriteLine(, item.ClassID, item.ClassName, item.Student.Name); } } }
运行结果:
外联接代码:
Query_OutJoin() { using (L2SDBDataContext db = new L2SDBDataContext()) { var query = from s in db.TStudents join c in db.TClasses on s.ClassID equals c.ClassID into gc from gci in gc.DefaultIfEmpty() { ClassID = s.ClassID, ClassName = gci.ClassName, Student = new { Name = s.Name, StudentID = s.StudentID } }; foreach (var item in query) { Console.WriteLine(, item.ClassID, item.ClassName, item.Student.Name); } } }
注意:outer join时必须将join后的表into到一个新的变量中,然后调用这个对象的DefaultIfEmpty方法。
运行结果与内联接相同。
posted on
,虚拟主机
Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to check my academic qualifications on Xuexin.com? You can check your academic qualifications on Xuexin.com, but many users don’t know how to check their academic qualifications on Xuexin.com. Next, the editor brings you a graphic tutorial on how to check your academic qualifications on Xuexin.com. Interested users come and take a look! Xuexin.com usage tutorial: How to check your academic qualifications on Xuexin.com 1. Xuexin.com entrance: https://www.chsi.com.cn/ 2. Website query: Step 1: Click on the Xuexin.com address above to enter the homepage Click [Education Query]; Step 2: On the latest webpage, click [Query] as shown by the arrow in the figure below; Step 3: Then click [Login Academic Credit File] on the new page; Step 4: On the login page Enter the information and click [Login];

Download the latest version of 12306 ticket booking app. It is a travel ticket purchasing software that everyone is very satisfied with. It is very convenient to go wherever you want. There are many ticket sources provided in the software. You only need to pass real-name authentication to purchase tickets online. All users You can easily buy travel tickets and air tickets and enjoy different discounts. You can also start booking reservations in advance to grab tickets. You can book hotels or special car transfers. With it, you can go where you want to go and buy tickets with one click. Traveling is simpler and more convenient, making everyone's travel experience more comfortable. Now the editor details it online Provides 12306 users with a way to view historical ticket purchase records. 1. Open Railway 12306, click My in the lower right corner, and click My Order 2. Click Paid on the order page. 3. On the paid page

The Xiaomi Mi 15 series is expected to be officially released in October, and its full series codenames have been exposed in the foreign media MiCode code base. Among them, the flagship Xiaomi Mi 15 Ultra is codenamed "Xuanyuan" (meaning "Xuanyuan"). This name comes from the Yellow Emperor in Chinese mythology, which symbolizes nobility. Xiaomi 15 is codenamed "Dada", while Xiaomi 15Pro is named "Haotian" (meaning "Haotian"). The internal code name of Xiaomi Mi 15S Pro is "dijun", which alludes to Emperor Jun, the creator god of "The Classic of Mountains and Seas". Xiaomi 15Ultra series covers

If you want to check the activation date using an Apple mobile phone, the best way is to check it through the serial number in the mobile phone. You can also check it by visiting Apple's official website, connecting it to a computer, and downloading third-party software to check it. How to check the activation date of Apple mobile phone Answer: Serial number query, Apple official website query, computer query, third-party software query 1. The best way for users is to know the serial number of their mobile phone. You can see the serial number by opening Settings, General, About This Machine. . 2. Using the serial number, you can not only know the activation date of your mobile phone, but also check the mobile phone version, mobile phone origin, mobile phone factory date, etc. 3. Users visit Apple's official website to find technical support, find the service and repair column at the bottom of the page, and check the iPhone activation information there. 4. User
![Outlook insists on trying to connect [Fix]](https://img.php.cn/upload/article/000/465/014/171029292689611.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
This article will explore the steps you can take to resolve situations where Outlook is stuck while trying to connect. No matter what version of Microsoft Office, including Microsoft 365, you may encounter this problem. This issue causes the Outlook desktop application to be unable to receive emails properly, so it becomes crucial to resolve this issue. Fix Outlook Hangs on Trying to Connect If Outlook is having trouble connecting and you find that you're not receiving new mail on the desktop app, the following suggestions may be able to help resolve the issue. Before trying these methods, check to see if your internet connection is working properly. Try restarting your router sometimes

WebSocket applications enable real-time two-way communication between client and server. Even though WebSocket connections are used, effective traffic management mechanisms are still required to avoid overloading the server, resulting in service interruption and unavailability. HAProxy is a free and reliable load balancer that also works as a reverse proxy. By configuring HAProxy to support WebSocket connections, you can better utilize the real-time data transmission characteristics of WebSockets and reduce the server load. This article will describe the detailed steps to configure HAProxy to support WebSocket connections. Step-by-step guide on how to configure HAProxy for WebSocket connections via WebSoc

Title: How to use Oracle to query whether a table is locked? In Oracle database, table lock means that when a transaction is performing a write operation on the table, other transactions will be blocked when they want to perform write operations on the table or make structural changes to the table (such as adding columns, deleting rows, etc.). In the actual development process, we often need to query whether the table is locked in order to better troubleshoot and deal with related problems. This article will introduce how to use Oracle statements to query whether a table is locked, and give specific code examples. To check whether the table is locked, we

Since the Huawei Mate60 series went on sale last year, I personally have been using the Mate60Pro as my main phone. In nearly a year, Huawei Mate60Pro has undergone multiple OTA upgrades, and the overall experience has been significantly improved, giving people a feeling of being constantly new. For example, recently, the Huawei Mate60 series has once again received a major upgrade in imaging capabilities. The first is the new AI elimination function, which can intelligently eliminate passers-by and debris and automatically fill in the blank areas; secondly, the color accuracy and telephoto clarity of the main camera have been significantly upgraded. Considering that it is the back-to-school season, Huawei Mate60 series has also launched an autumn promotion: you can enjoy a discount of up to 800 yuan when purchasing the phone, and the starting price is as low as 4,999 yuan. Commonly used and often new products with great value
