全て
| カテゴリ未分類
| 通常
| 数学
| 投資
| 副業
| 転職・就職
| ブログ
| 株Stock
| VBA
| SharePoint
| REST API
| RenderListDataAsStream
| PowerAutomateDesktop
| Graph API
| PowerApps
| PowerAutomate
| PowerFx
| OfficeScriptオフィススクリプト
| PowerAutomate日時Excel日時相互関係
| PowerShell
| Dataverse
| UiPath
| OneDrive
| LINQ
| VB.NET
| C#.NET
| WPFC#.NET
| Git
カテゴリ:LINQ
using System; using System.Data; using System.Linq; class Program { static void Main(string[] args) { // サンプルのDataTable1を作成します DataTable table1 = new DataTable(); table1.Columns.Add("ID", typeof(int)); table1.Columns.Add("Name", typeof(string)); table1.Rows.Add(1, "John"); table1.Rows.Add(2, "Alice"); table1.Rows.Add(3, "Bob"); // サンプルのDataTable2を作成します DataTable table2 = new DataTable(); table2.Columns.Add("ID", typeof(int)); table2.Columns.Add("Age", typeof(int)); table2.Rows.Add(1, 30); table2.Rows.Add(3, 25); table2.Rows.Add(4, 35); // サンプルのDataTable3を作成します DataTable table3 = new DataTable(); table3.Columns.Add("ID", typeof(int)); table3.Columns.Add("Department", typeof(string)); table3.Rows.Add(1, "IT"); table3.Rows.Add(2, "HR"); table3.Rows.Add(3, "Finance"); // 外部結合を行います var query = from row1 in table1.AsEnumerable() join row2 in table2.AsEnumerable() on row1.Field<int>("ID") equals row2.Field<int>("ID") into temp from t1 in temp.DefaultIfEmpty() join row3 in table3.AsEnumerable() on row1.Field<int>("ID") equals row3.Field<int>("ID") into temp2 from t2 in temp2.DefaultIfEmpty() select new { ID = row1.Field<int>("ID"), Name = row1.Field<string>("Name"), Age = t1 != null ? t1.Field<int>("Age") : 0, Department = t2 != null ? t2.Field<string>("Department") : "N/A" }; // 結果を出力します foreach (var item in query) { Console.WriteLine($"ID: {item.ID}, Name: {item.Name}, Age: {item.Age}, Department: {item.Department}"); } } } お気に入りの記事を「いいね!」で応援しよう
Last updated
2024.02.25 07:41:54
[LINQ] カテゴリの最新記事
|