2024/02/24(土)07:06
C#でLinQを使用してDatatableから列1と列2と列3の値を取得するコード
using System;
using System.Data;
using System.Linq;
class Program
{
static void Main()
{
// サンプルのDataTableを作成する
DataTable table = new DataTable();
table.Columns.Add("列1", typeof(int));
table.Columns.Add("列2", typeof(string));
table.Columns.Add("列3", typeof(double));
// ダミーデータを追加する
table.Rows.Add(1, "A", 10.5);
table.Rows.Add(2, "B", 20.7);
table.Rows.Add(3, "C", 30.9);
// LINQを使用して列1、列2、列3の値を取得する
var queryResult = from DataRow row in table.Rows
select new
{
列1 = row.Field<int>("列1"),
列2 = row.Field<string>("列2"),
列3 = row.Field<double>("列3")
};
// 結果を出力する
foreach (var item in queryResult)
{
Console.WriteLine($"列1: {item.列1}, 列2: {item.列2}, 列3: {item.列3}");
}
}
}