Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

C# DataTable 型を便利にする拡張メソッドを作ってみた

0
Posted at

はじめに

備忘録です。DataTable 型を便利にする拡張メソッドを作ってみました。

環境

  • Visual Studio Community 2022
  • .NET 8.0

前提プログラム

過去記事「C# 型変換のヘルパークラスを作ってみた」の上に成り立つプログラムです。そちらもご参照ください。

DataTable 型の拡張メソッド

DataTableExtensions.cs
/// <summary>
/// <see cref="DataTable"/> 型の拡張メソッドを提供する静的クラスです。
/// </summary>
public static class DataTableExtensions
{
    /// <summary>
    /// 指定した <see cref="DataTable"/> が null であるか、行が存在しないかを示す値を返します。
    /// </summary>
    /// <param name="table">対象の DataTable 型。</param>
    /// <returns>指定した <see cref="DataTable"/> が null であるか、行が存在しない場合は true、それ以外の場合は false を返します。</returns>
    public static bool IsNullOrEmpty(this DataTable? table)
    {
        return table == null || table.Rows.Count == 0;
    }

    /// <summary>
    /// DataTable の指定したセルの値を文字列として取得します。取得に失敗した場合は、指定した既定値を返します。
    /// </summary>
    /// <param name="table">対象の DataTable 型。</param>
    /// <param name="rowIndex">取得する行のインデックス。</param>
    /// <param name="columnIndex">取得する列のインデックス。</param>
    /// <param name="defaultValue">取得失敗した場合に返す既定値。</param>
    /// <returns>取得に成功した場合は文字列、失敗した場合は規定値を返します。</returns>
    public static string GetStringOrDefault(
        this DataTable? table,
        int rowIndex,
        int columnIndex,
        string defaultValue = "")
    {
        if (table.IsNullOrEmpty())
        {
            return defaultValue;
        }

        if (rowIndex < 0 || rowIndex >= table!.Rows.Count)
        {
            return defaultValue;
        }

        if (columnIndex < 0 || columnIndex >= table.Columns.Count)
        {
            return defaultValue;
        }

        return table
            .Rows[rowIndex][columnIndex]
            .ToStringOrDefault(defaultValue);
    }

    /// <summary>
    /// DataTable の指定したセルの値を文字列として取得します。取得に失敗した場合は、指定した既定値を返します。
    /// </summary>
    /// <param name="table">対象の DataTable 型。</param>
    /// <param name="rowIndex">取得する行のインデックス。</param>
    /// <param name="columnName">取得する列の名前。</param>
    /// <param name="defaultValue">取得失敗した場合に返す既定値。</param>
    /// <returns>取得に成功した場合は文字列、失敗した場合は規定値を返します。</returns>
    public static string GetStringOrDefault(
        this DataTable? table,
        int rowIndex,
        string columnName,
        string defaultValue = "")
    {
        if (table.IsNullOrEmpty())
        {
            return defaultValue;
        }

        if (rowIndex < 0 || rowIndex >= table!.Rows.Count)
        {
            return defaultValue;
        }

        if (!table.Columns.Contains(columnName))
        {
            return defaultValue;
        }

        return table
            .Rows[rowIndex][columnName]
            .ToStringOrDefault(defaultValue);
    }

    /// <summary>
    /// 指定した <see cref="DataTable"/> の行を列挙可能なコレクションとして返します。
    /// </summary>
    /// <param name="table">対象の DataTable 型。</param>
    /// <returns>指定した <see cref="DataTable"/> の行を列挙可能なコレクションとして返します。</returns>
    public static IEnumerable<DataRow> AsEnumerableSafe(this DataTable? table)
    {
        #pragma warning disable IDE0301
        return table?.Rows.Cast<DataRow>() ?? Enumerable.Empty<DataRow>();
        #pragma warning restore IDE0301
    }

    /// <summary>
    /// 指定した <see cref="DataTable"/> の行をリストとして返します。
    /// </summary>
    /// <param name="table">対象の DataTable 型。</param>
    /// <returns>指定した <see cref="DataTable"/> の行をリストとして返します。</returns>
    public static List<DataRow> ToDataRowList(this DataTable? table) => [.. table.AsEnumerableSafe()];
}

おわりに

DataTable も最近はめっきり使わなくなりました。

0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?