SQL、LINQ、Lambda 三種用法(轉)

来源:https://www.cnblogs.com/liujianshe1990-/archive/2019/03/27/10606748.html
-Advertisement-
Play Games

SQL、LINQ、Lambda 三種用法顏色註釋: SQL LinqToSql Lambda QA1、 查詢Student表中的所有記錄的Sname、Ssex和Class列。select sname,ssex,class from studentLinq: from s in Students se ...


SQL、LINQ、Lambda 三種用法
顏色註釋: SQL LinqToSql Lambda QA
1、 查詢Student表中的所有記錄的Sname、Ssex和Class列。
select sname,ssex,class from student
Linq:
from s in Students
select new {
s.SNAME,
s.SSEX,
s.CLASS
}
Lambda:
Students.Select( s => new {
SNAME = s.SNAME,SSEX = s.SSEX,CLASS = s.CLASS
})

2、 查詢教師所有的單位即不重覆的Depart列。
select distinct depart from teacher
Linq:
from t in Teachers.Distinct()
select t.DEPART
Lambda:
Teachers.Distinct().Select( t => t.DEPART)
3、 查詢Student表的所有記錄。
select * from student
Linq:
from s in Students
select s
Lambda:
Students.Select( s => s)

4、 查詢Score表中成績在60到80之間的所有記錄。
select * from score where degree between 60 and 80
Linq:
from s in Scores
where s.DEGREE >= 60 && s.DEGREE < 80
select s
Lambda:
Scores.Where(
s => (
s.DEGREE >= 60 && s.DEGREE < 80
)
)
5、 查詢Score表中成績為85,86或88的記錄。
select * from score where degree in (85,86,88)
Linq:
In
from s in Scores
where (
new decimal[]{85,86,88}
).Contains(s.DEGREE)
select s
Lambda:
Scores.Where( s => new Decimal[] {85,86,88}.Contains(s.DEGREE))
Not in
from s in Scores
where !(
new decimal[]{85,86,88}
).Contains(s.DEGREE)
select s
Lambda:
Scores.Where( s => !(new Decimal[]{85,86,88}.Contains(s.DEGREE)))
Any()應用:雙表進行Any時,必須是主鍵為(String)
CustomerDemographics CustomerTypeID(String)
CustomerCustomerDemos (CustomerID CustomerTypeID) (String)
一個主鍵與二個主建進行Any(或者是一對一關鍵進行Any)
不可,以二個主鍵於與一個主鍵進行Any

from e in CustomerDemographics
where !e.CustomerCustomerDemos.Any()
select e

from c in Categories
where !c.Products.Any()
select c
6、 查詢Student表中"95031"班或性別為"女"的同學記錄。
select * from student where class ='95031' or ssex= N'女'
Linq:
from s in Students
where s.CLASS == "95031"
|| s.CLASS == "女"
select s
Lambda:
Students.Where(s => ( s.CLASS == "95031" || s.CLASS == "女"))

7、 以Class降序查詢Student表的所有記錄。
select * from student order by Class DESC
Linq:
from s in Students
orderby s.CLASS descending
select s
Lambda:
Students.OrderByDescending(s => s.CLASS)
8、 以Cno升序、Degree降序查詢Score表的所有記錄。
select * from score order by Cno ASC,Degree DESC
Linq:(這裡Cno ASC在linq中要寫在最外面)
from s in Scores
orderby s.DEGREE descending
orderby s.CNO ascending
select s
Lambda:
Scores.OrderByDescending( s => s.DEGREE)
.OrderBy( s => s.CNO)

9、 查詢"95031"班的學生人數。
select count(*) from student where class = '95031'
Linq:
( from s in Students
where s.CLASS == "95031"
select s
).Count()
Lambda:
Students.Where( s => s.CLASS == "95031" )
.Select( s => s)
.Count()
10、查詢Score表中的最高分的學生學號和課程號。
select distinct s.Sno,c.Cno from student as s,course as c ,score as sc
where s.sno=(select sno from score where degree = (select max(degree) from score))
and c.cno = (select cno from score where degree = (select max(degree) from score))
Linq:
(
from s in Students
from c in Courses
from sc in Scores
let maxDegree = (from sss in Scores
select sss.DEGREE
).Max()
let sno = (from ss in Scores
where ss.DEGREE == maxDegree
select ss.SNO).Single().ToString()
let cno = (from ssss in Scores
where ssss.DEGREE == maxDegree
select ssss.CNO).Single().ToString()
where s.SNO == sno && c.CNO == cno
select new {
s.SNO,
c.CNO
}
).Distinct()
操作時問題?執行時報錯: where s.SNO == sno(這行報出來的) 運算符"=="無法應用於"string"和"System.Linq.IQueryable<string>"類型的操作數
解決:
原:let sno = (from ss in Scores
where ss.DEGREE == maxDegree
select ss.SNO).ToString()
Queryable().Single()返回序列的唯一元素;如果該序列並非恰好包含一個元素,則會引發異常。
解:let sno = (from ss in Scores
where ss.DEGREE == maxDegree
select ss.SNO).Single().ToString()
11、查詢'3-105'號課程的平均分。
select avg(degree) from score where cno = '3-105'
Linq:
(
from s in Scores
where s.CNO == "3-105"
select s.DEGREE
).Average()
Lambda:
Scores.Where( s => s.CNO == "3-105")
.Select( s => s.DEGREE)
.Average()

12、查詢Score表中至少有5名學生選修的並以3開頭的課程的平均分數。
select avg(degree) from score where cno like '3%' group by Cno having count(*)>=5
Linq:
from s in Scores
where s.CNO.StartsWith("3")
group s by s.CNO
into cc
where cc.Count() >= 5
select cc.Average( c => c.DEGREE)
Lambda:
Scores.Where( s => s.CNO.StartsWith("3") )
.GroupBy( s => s.CNO )
.Where( cc => ( cc.Count() >= 5) )
.Select( cc => cc.Average( c => c.DEGREE) )
Linq: SqlMethod
like也可以這樣寫:
s.CNO.StartsWith("3") or SqlMethods.Like(s.CNO,"%3")
13、查詢最低分大於70,最高分小於90的Sno列。
select sno from score group by sno having min(degree) > 70 and max(degree) < 90
Linq:
from s in Scores
group s by s.SNO
into ss
where ss.Min(cc => cc.DEGREE) > 70 && ss.Max( cc => cc.DEGREE) < 90
select new
{
sno = ss.Key
}
Lambda:
Scores.GroupBy (s => s.SNO)
.Where (ss => ((ss.Min (cc => cc.DEGREE) > 70) && (ss.Max (cc => cc.DEGREE) < 90)))
.Select ( ss => new {
sno = ss.Key
})
14、查詢所有學生的Sname、Cno和Degree列。
select s.sname,sc.cno,sc.degree from student as s,score as sc where s.sno = sc.sno
Linq:
from s in Students
join sc in Scores
on s.SNO equals sc.SNO
select new
{
s.SNAME,
sc.CNO,
sc.DEGREE
}
Lambda:
Students.Join(Scores, s => s.SNO,
sc => sc.SNO,
(s,sc) => new{
SNAME = s.SNAME,
CNO = sc.CNO,
DEGREE = sc.DEGREE
})
15、查詢所有學生的Sno、Cname和Degree列。
select sc.sno,c.cname,sc.degree from course as c,score as sc where c.cno = sc.cno
Linq:
from c in Courses
join sc in Scores
on c.CNO equals sc.CNO
select new
{
sc.SNO,c.CNAME,sc.DEGREE
}
Lambda:
Courses.Join ( Scores, c => c.CNO,
sc => sc.CNO,
(c, sc) => new
{
SNO = sc.SNO,
CNAME = c.CNAME,
DEGREE = sc.DEGREE
})
16、查詢所有學生的Sname、Cname和Degree列。
select s.sname,c.cname,sc.degree from student as s,course as c,score as sc where s.sno = sc.sno and c.cno = sc.cno
Linq:
from s in Students
from c in Courses
from sc in Scores
where s.SNO == sc.SNO && c.CNO == sc.CNO
select new { s.SNAME,c.CNAME,sc.DEGREE }


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 筆記記錄自林曉斌(丁奇)老師的《MySQL實戰45講》 (本篇內圖片均來自丁奇老師的講解,如有侵權,請聯繫我刪除) 12) --為什麼我的MySQL會“抖”一下? 斷更了一段時間,因為這幾周實在是太忙了,周末加班兩天那種。。。 有時你會遇到這樣的問題,一條SQL語句,正常執行的時候很快,但是有時候會 ...
  • 在mysql的命令行模式中,通過insert語句插入中文數據的時候報錯,類似於下麵這樣: 造成這個錯誤通常是由於創建數據表的時候使用了不正確的編碼格式,可以使用如下命令查看操作的目標數據表的編碼格式。 通過結果就可以看到目標表的目標欄位對應的編碼格式,我們只需要把該列的編碼格式轉化為 即可。 當然這 ...
  • 下載 將文件下載到CentOS伺服器上,或者通過Windows下載後上傳到CentOS伺服器上。 (這裡將文件放到/opt/soft/mysql-5.7.25-el7-x86_64.tar.gz) 安裝配置 1、添加組和用戶 [root@localhost ~]: groupadd mysql [r ...
  • 什麼是rowkey Hbase是一個分散式的、面向列的資料庫,它和一般關係型資料庫的最大區別是:HBase很適合於存儲非結構化的數據,還有就是它基於列的而不是基於行的模式. Hbase是採用K,V存儲的,那Rowkey就是KeyValue的Key了,Rowkey也是一段二進位碼流,最大長度為64KB ...
  • 當時從主庫通過rman備份到目前測試庫還原之後,由於備份是在備庫備份的,所以資料庫還原後狀態為readonly,standby_file_management參數為auto。首先需要通過alter database clear logfile group 日誌組;讓資料庫在磁碟創建日誌文件。出現問題 ...
  • 此篇承接上一篇的基本原理,繼續展開學習,本篇主要面向數據的使用和管理,也就是開發者常用的基礎語句,開始嘍…… >>>對整表的操作 >創建表 關鍵字 Create create table student( stu_id int primary key, stu_name varchar2(20) n ...
  • 一、一些基本定義 數據:是事物客觀存在的反映,可以用文字、符號等記錄下來的。 信息:數據+處理 經過加工處理的有用的數據。 欄位:標識實體屬性的命名單位。 記錄:欄位的有序集合,一個記錄描述一個實體。 關鍵字:唯一標識每個記錄的欄位或欄位集。 二、SQL SERVER 架構 SQL Server是基 ...
  • 網路配置、遠程連接配置: 防火牆設置: SQL Server的預設埠號是1433。 網路配置: SQLServer Configuration Manager中的客戶端協議,眾多IP中隨便選一個,比如我選了“IP10”然後IP地址改為現在使用的網路的IP地址,然後把它啟用。可以觀察到預設狀態下所有 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...