SQL、LINQ、ラムダ式
まず、これら 3 つのまったく異なるものについて説明します。SQL は、誰もがよく知っている Structured Query Language の略称であり、主に LINQ 式と Lambda 式の基本的な概念と、これら 3 つの異なる実装について説明します。同じクエリに対して。
簡単な紹介
LINQ (Language Integrate Query) は、オブジェクトとデータ間の対応関係を確立する言語統合クエリです。メモリ オブジェクトにアクセスすることで、データ コレクションをクエリできます。 LINQ クエリは、C# の言語構造です。したがって、開発者は、C# コード内の SQL ステートメントと同様のクエリ式をネストして、データ クエリ関数を実装できます。 LINQ は、単に C# の入れ子になったクエリ式ではなく、クエリ式を C# の構文として使用します。
.NET クラス ライブラリでは、LINQ 関連のクラス ライブラリは System.Linq 名前空間の下にあります。この名前空間は、LINQ を使用したクエリをサポートするクラスとインターフェイスを提供します。その中で最も重要なものは、次の 2 つのクラスと 2 つのインターフェイスです。
※IEnumerable インターフェイス: クエリ可能なデータ コレクションを表します。クエリは通常、コレクション内の要素を 1 つずつフィルターし、クエリ結果を保存するための新しい IEnumerable インターフェイスを返します。
※IQueryableインターフェース:IEnumerableインターフェースを継承し、クエリ可能な式ディレクトリツリーを表します。
※Enumerableクラス:IEnumerableの拡張メソッドを提供することでLINQ標準クエリ演算子を実装します。受け渡し、ナビゲーション、並べ替え、クエリ、結合、合計、最大値、最小値などの操作が含まれます。
※Queryableクラス:IQueryableの拡張メソッドを提供することでLINQ標準クエリ演算子を実装します。受け渡し、ナビゲーション、並べ替え、クエリ、結合、合計、最大値、最小値などの操作が含まれます。
ラムダ式は実際には匿名関数であり、LINQ の補足と言えます。 LINQ クエリ キーワードと IEnumerable インターフェイスのメソッドの間には対応関係があるため、LINQ クエリ式で使用できるクエリ関数はほとんどありません。実際の開発では、クエリ結果またはデータ ソースを通じてメソッド呼び出しが行われ、さらにクエリ操作が実行されます。ラムダ式は匿名関数であるため、デリゲートに割り当てることができます。また、IEnumerable インターフェイスのメソッドの多くは関数デリゲートを使用してカスタム操作、条件、その他の操作を実装するため、ラムダ式は LINQ で広く使用されています。
比較実装
※すべてのコンテンツをクエリ
1 查询Student表的所有记录。 2 select * from student 3 Linq: 4 from s in Students 5 select s 6 Lambda: 7 Students.Select( s => s)
※列ごとにクエリ
select sname,ssex,class from student 3 Linq: 4 from s in Students 5 select new { 6 s.SNAME, 7 s.SSEX, 8 s.CLASS 9 } 10 Lambda: 11 Students.Select( s => new { 12 SNAME = s.SNAME,SSEX = s.SSEX,CLASS = s.CLASS 13 })
※個別の重複排除クエリ
查询教师所有的单位即不重复的Depart列。 2 select distinct depart from teacher 3 Linq: 4 from t in Teachers.Distinct() 5 select t.DEPART6 Lambda: 7 Teachers.Distinct().Select( t => t.DEPART)
※2つの間隔内のクエリ
1 查询Score表中成绩在60到80之间的所有记录。 2 select * from score where degree between 60 and 80 3 Linq: 4 from s in Scores 5 where s.DEGREE >= 60 && s.DEGREE < 80 6 select s 7 Lambda: 8 Scores.Where( 9 s => ( 10 s.DEGREE >= 60 && s.DEGREE < 80 11 ) 12 )
※範囲内のクエリ
select * from score where degree in (85,86,88) 2 Linq: 3 from s in Scores 4 where ( 5 new decimal[]{85,86,88} 6 ).Contains(s.DEGREE) 7 select s 8 Lambda: 9 Scores.Where( s => new Decimal[] {85,86,88}.Contains(s.DEGREE))
※または関係クエリ
查询Student表中"95031"班或性别为"女"的同学记录。 2 select * from student where class ='95031' or ssex= N'女' 3 Linq: 4 from s in Students 5 where s.CLASS == "95031" 6 || s.CLASS == "女" 7 select s 8 Lambda: 9 Students.Where(s => ( s.CLASS == "95031" || s.CLASS == "女"))
※ ソート
以Class降序查询Student表的所有记录。 2 select * from student order by Class DESC 3 Linq: 4 from s in Students 5 orderby s.CLASS descending 6 select s 7 Lambda: 8 Students.OrderByDescending(s => s.CLASS)
※ 行番号クエリ
select count(*) from student where class = '95031' 2 Linq: 3 ( from s in Students 4 where s.CLASS == "95031" 5 select s 6 ).Count() 7 Lambda: 8 Students.Where( s => s.CLASS == "95031" ) 9 .Select( s => s) 10 .Count()
※ 平均クエリ
查询'3-105'号课程的平均分。 2 select avg(degree) from score where cno = '3-105' 3 Linq: 4 ( 5 from s in Scores 6 where s.CNO == "3-105" 7 select s.DEGREE 8 ).Average() 9 Lambda: 10 Scores.Where( s => s.CNO == "3-105") 11 .Select( s => s.DEGREE)
※ 非一致クエリ
查询Score表中的最高分的学生学号和课程号。 2 select distinct s.Sno,c.Cno from student as s,course as c ,score as sc 3 where s.sno=(select sno from score where degree = (select max(degree) from score)) 4 and c.cno = (select cno from score where degree = (select max(degree) from score)) 5 Linq: 6 ( 7 from s in Students 8 from c in Courses 9 from sc in Scores 10 let maxDegree = (from sss in Scores 11 select sss.DEGREE 12 ).Max() 13 let sno = (from ss in Scores 14 where ss.DEGREE == maxDegree 15 select ss.SNO).Single().ToString() 16 let cno = (from ssss in Scores 17 where ssss.DEGREE == maxDegree 18 select ssss.CNO).Single().ToString() 19 where s.SNO == sno && c.CNO == cno 20 select new { 21 s.SNO, 22 c.CNO 23 } 24 ).Distinct()
※ グループ化
查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。 2 select avg(degree) from score where cno like '3%' group by Cno having count(*)>=5 3 Linq: 4 from s in Scores 5 where s.CNO.StartsWith("3") 6 group s by s.CNO 7 into cc 8 where cc.Count() >= 5 9 select cc.Average( c => c.DEGREE) 10 Lambda: 11 Scores.Where( s => s.CNO.StartsWith("3") ) 12 .GroupBy( s => s.CNO ) 13 .Where( cc => ( cc.Count() >= 5) ) 14 .Select( cc => cc.Average( c => c.DEGREE) ) 15 Linq: SqlMethod 16 like也可以这样写: 17 s.CNO.StartsWith("3") or SqlMethods.Like(s.CNO,"%3")
※ グループ化フィルタリング
查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。 2 select avg(degree) from score where cno like '3%' group by Cno having count(*)>=5 3 Linq: 4 from s in Scores 5 where s.CNO.StartsWith("3") 6 group s by s.CNO 7 into cc 8 where cc.Count() >= 5 9 select cc.Average( c => c.DEGREE) 10 Lambda: 11 Scores.Where( s => s.CNO.StartsWith("3") ) 12 .GroupBy( s => s.CNO ) 13 .Where( cc => ( cc.Count() >= 5) ) 14 .Select( cc => cc.Average( c => c.DEGREE) ) 15 Linq: SqlMethod 16 like也可以这样写: 17 s.CNO.StartsWith("3") or SqlMethods.Like(s.CNO,"%3")
※ 複数テーブル結合クエリ
select sc.sno,c.cname,sc.degree from course as c,score as sc where c.cno = sc.cno 2 Linq: 3 from c in Courses 4 join sc in Scores 5 on c.CNO equals sc.CNO 6 select new 7 { 8 sc.SNO,c.CNAME,sc.DEGREE 9 } 10 Lambda: 11 Courses.Join ( Scores, c => c.CNO, 12 sc => sc.CNO, 13 (c, sc) => new 14 { 15 SNO = sc.SNO, 16 CNAME = c.CNAME, 17 DEGREE = sc.DEGREE 18 }) 19 .Average()
上記の内容は情報を確認してまとめました。不足点があれば批判して修正してください。
まず、これら 3 つのまったく異なるものについて説明します。SQL は、誰もがよく知っている Structured Query Language の略称であり、主に LINQ 式と Lambda 式の基本的な概念と、同じクエリのさまざまな実装について説明します。この3つ。
简单介绍
LINQ(Language Integrate Query)是语言集成查询他在对象和数据之间建立一种对应的关系,可以使用访问内存对象的方式查询数据集合。LINQ查询是C#中的一种语言构造。因此开发人员可以再C#代码汇总嵌套类似于SQL语句的查询表达式,从而实现数据查询的功能。LINQ也不是简单地作为C#中嵌套查询表达式,而是将查询表达式作为C#的一种语法。
在.NET类库中,LINQ相关类库都在System.Linq命名空间下,该命名空间提供支持使用LINQ进行查询的类和接口,其中最主要的是以下两个类和两个接口。
※IEnumerable接口:它表示可以查询的数据集合,一个查询通常是逐个对集合中的元素进行筛选操作,返回一个新的IEnumerable接口,用来保存查询结果。
※IQueryable接口:他继承IEnumerable接口,表示一个可以查询的表达式目录树。
※Enumerable类:它通过对IEnumerable提供扩展方法,实现LINQ标准查询运算符。包括过路、导航、排序、查询、联接、求和、求最大值、求最小值等操作。
※Queryable类:它通过对IQueryable提供扩展方法,实现LINQ标准查询运算符。包括过路、导航、排序、查询、联接、求和、求最大值、求最小值等操作。
Lambda表达式实际上是一个匿名函数,它可以说是对LINQ的补充。由于LINQ查询关键字和IEnumerable接口的方法之间有一个对应关系,但是LINQ查询表达式中可以使用的查询功能很少。在实际开发中通过查询结果或数据源进行方法调用,从而进行更多的查询操作。由于Lambda表达式是匿名函数,它可以赋值到一个委托,而在IEnumerable接口的方法中很多通过函数委托来实现自定义运算、条件等操作,所以Lambda表达式在LINQ中被广泛使用。
对比实现
※查询全部内容
1 查询Student表的所有记录。 2 select * from student 3 Linq: 4 from s in Students 5 select s 6 Lambda: 7 Students.Select( s => s)
※按列查询
select sname,ssex,class from student 3 Linq: 4 from s in Students 5 select new { 6 s.SNAME, 7 s.SSEX, 8 s.CLASS 9 } 10 Lambda: 11 Students.Select( s => new { 12 SNAME = s.SNAME,SSEX = s.SSEX,CLASS = s.CLASS 13 })
※distinct去重查询
查询教师所有的单位即不重复的Depart列。 2 select distinct depart from teacher 3 Linq: 4 from t in Teachers.Distinct() 5 select t.DEPART 6 Lambda: 7 Teachers.Distinct().Select( t => t.DEPART)
※两个区间内查询
1 查询Score表中成绩在60到80之间的所有记录。 2 select * from score where degree between 60 and 80 3 Linq: 4 from s in Scores 5 where s.DEGREE >= 60 && s.DEGREE < 80 6 select s 7 Lambda: 8 Scores.Where( 9 s => ( 10 s.DEGREE >= 60 && s.DEGREE < 80 11 ) 12 )
※在一个范围内查询
select * from score where degree in (85,86,88) 2 Linq: 3 from s in Scores 4 where ( 5 new decimal[]{85,86,88} 6 ).Contains(s.DEGREE) 7 select s 8 Lambda: 9 Scores.Where( s => new Decimal[] {85,86,88}.Contains(s.DEGREE))
※或关系查询
查询Student表中"95031"班或性别为"女"的同学记录。 2 select * from student where class ='95031' or ssex= N'女' 3 Linq: 4 from s in Students 5 where s.CLASS == "95031" 6 || s.CLASS == "女" 7 select s 8 Lambda: 9 Students.Where(s => ( s.CLASS == "95031" || s.CLASS == "女"))
※排序
以Class降序查询Student表的所有记录。 2 select * from student order by Class DESC 3 Linq: 4 from s in Students 5 orderby s.CLASS descending 6 select s 7 Lambda: 8 Students.OrderByDescending(s => s.CLASS)
※行数查询
select count(*) from student where class = '95031' 2 Linq: 3 ( from s in Students 4 where s.CLASS == "95031" 5 select s 6 ).Count() 7 Lambda: 8 Students.Where( s => s.CLASS == "95031" ) 9 .Select( s => s) 10 .Count()
※平均值查询
查询'3-105'号课程的平均分。 2 select avg(degree) from score where cno = '3-105' 3 Linq: 4 ( 5 from s in Scores 6 where s.CNO == "3-105" 7 select s.DEGREE 8 ).Average() 9 Lambda: 10 Scores.Where( s => s.CNO == "3-105") 11 .Select( s => s.DEGREE)
※潜逃查询
查询Score表中的最高分的学生学号和课程号。 2 select distinct s.Sno,c.Cno from student as s,course as c ,score as sc 3 where s.sno=(select sno from score where degree = (select max(degree) from score)) 4 and c.cno = (select cno from score where degree = (select max(degree) from score)) 5 Linq: 6 ( 7 from s in Students 8 from c in Courses 9 from sc in Scores 10 let maxDegree = (from sss in Scores 11 select sss.DEGREE 12 ).Max() 13 let sno = (from ss in Scores 14 where ss.DEGREE == maxDegree 15 select ss.SNO).Single().ToString() 16 let cno = (from ssss in Scores 17 where ssss.DEGREE == maxDegree 18 select ssss.CNO).Single().ToString() 19 where s.SNO == sno && c.CNO == cno 20 select new { 21 s.SNO, 22 c.CNO 23 } 24 ).Distinct()
※分组
查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。 2 select avg(degree) from score where cno like '3%' group by Cno having count(*)>=5 3 Linq: 4 from s in Scores 5 where s.CNO.StartsWith("3") 6 group s by s.CNO 7 into cc 8 where cc.Count() >= 5 9 select cc.Average( c => c.DEGREE) 10 Lambda: 11 Scores.Where( s => s.CNO.StartsWith("3") ) 12 .GroupBy( s => s.CNO ) 13 .Where( cc => ( cc.Count() >= 5) ) 14 .Select( cc => cc.Average( c => c.DEGREE) ) 15 Linq: SqlMethod 16 like也可以这样写: 17 s.CNO.StartsWith("3") or SqlMethods.Like(s.CNO,"%3")
※分组过滤
查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。 2 select avg(degree) from score where cno like '3%' group by Cno having count(*)>=5 3 Linq: 4 from s in Scores 5 where s.CNO.StartsWith("3") 6 group s by s.CNO 7 into cc 8 where cc.Count() >= 5 9 select cc.Average( c => c.DEGREE) 10 Lambda: 11 Scores.Where( s => s.CNO.StartsWith("3") ) 12 .GroupBy( s => s.CNO ) 13 .Where( cc => ( cc.Count() >= 5) ) 14 .Select( cc => cc.Average( c => c.DEGREE) ) 15 Linq: SqlMethod 16 like也可以这样写: 17 s.CNO.StartsWith("3") or SqlMethods.Like(s.CNO,"%3")
※多表联合查询
select sc.sno,c.cname,sc.degree from course as c,score as sc where c.cno = sc.cno 2 Linq: 3 from c in Courses 4 join sc in Scores 5 on c.CNO equals sc.CNO 6 select new 7 { 8 sc.SNO,c.CNAME,sc.DEGREE 9 } 10 Lambda: 11 Courses.Join ( Scores, c => c.CNO, 12 sc => sc.CNO, 13 (c, sc) => new 14 { 15 SNO = sc.SNO, 16 CNAME = c.CNAME, 17 DEGREE = sc.DEGREE 18 }) 19 .Average()
以上就是SQL、LINQ和Lambda表达式的内容,更多相关内容请关注PHP中文网(www.php.cn)!

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック









C++ では、Lambda 式を使用して例外を処理する方法が 2 つあります。try-catch ブロックを使用して例外をキャッチする方法と、catch ブロックで例外を処理または再スローする方法です。 std::function 型のラッパー関数を使用すると、その try_emplace メソッドで Lambda 式の例外をキャッチできます。

C++ では、クロージャは外部変数にアクセスできるラムダ式です。クロージャを作成するには、ラムダ式の外部変数をキャプチャします。クロージャには、再利用性、情報の隠蔽、評価の遅延などの利点があります。これらは、イベント ハンドラーなど、外部変数が破棄されてもクロージャが外部変数にアクセスできる現実の状況で役立ちます。

C++ マルチスレッド プログラミングにおけるラムダ式の利点には、シンプルさ、柔軟性、パラメータの受け渡しの容易さ、並列処理が含まれます。実際のケース: ラムダ式を使用してマルチスレッドを作成し、異なるスレッドでスレッド ID を出力します。これは、このメソッドのシンプルさと使いやすさを示しています。

C++ ラムダ式は、関数スコープ変数を保存し、関数からアクセスできるようにするクロージャーをサポートしています。構文は [キャプチャリスト](パラメータ)->戻り値の型{関数本体} です。 Capture-list は、キャプチャする変数を定義します。[=] を使用してすべてのローカル変数を値によってキャプチャするか、[&] を使用してすべてのローカル変数を参照によってキャプチャするか、[variable1, variable2,...] を使用して特定の変数をキャプチャできます。ラムダ式はキャプチャされた変数にのみアクセスできますが、元の値を変更することはできません。

C++ で外部変数のラムダ式をキャプチャするには、次の 3 つの方法があります。 値によるキャプチャ: 変数のコピーを作成します。参照によるキャプチャ: 変数参照を取得します。値と参照による同時キャプチャ: 値または参照による複数の変数のキャプチャを許可します。

C++ では、ラムダ式を関数パラメータとして使用して、コールバック関数の柔軟性を実現できます。具体的には: パラメーターの受け渡し: std::function を介して Lambda 式をラップし、関数ポインターの形式で関数に渡します。戻り値の処理: std::functionでコールバック関数のポインタを宣言する際の戻り値の型を指定します。実践的なケース: GUI イベント処理のコールバックを最適化し、不要なオブジェクトや関数ポインターの作成を回避し、コードの単純さと保守性を向上させます。

C++ ラムダ式を使用して遅延評価を実行するにはどうすればよいですか?ラムダ式を使用して、遅延評価される関数オブジェクトを作成します。遅延計算により、必要になるまで実行が延期されます。必要な場合にのみ結果を計算し、パフォーマンスを向上させます。

C++ ラムダ式を最適化するためのパフォーマンスのヒントは次のとおりです。 ラムダ オブジェクトの不必要な作成を回避する std::bind を介して最小のオブジェクトを明示的にキャプチャする コピーを避けるために std::move を使用してキャプチャされた変数を移動する 不必要なメモリ割り当て、計算の繰り返しを回避するためにラムダ本体を最適化するグローバル変数へのアクセス
