Skip to content

[API Proposal]: DataTable.LoadDataRow with array length greater than the number of columns #123758

@0UserName

Description

@0UserName

Background and motivation

LoadDataRow into DataTable, or more precisely, into NewRecordFromArray the check is performed to ensure that the length of the array containing the row values ​​does not exceed the number of columns:

if (colCount < value.Length)
{
    throw ExceptionBuilder.ValueArrayLength();
}

Because of this, we cannot use built-in solutions like ArrayPool.Shared to pass row values, since the pool does not guarantee the exact length of the returned array.

In this case, we are forced to either perform explicit allocations without using a pool, or implement a custom pool that returns arrays with exact lengths. However, in scenarios where the number of columns and consequently, the required array length varies dynamically, the number of such pools becomes unbounded.

API Proposal

public DataRow LoadDataRow(ReadOnlySpan<object> values, bool fAcceptChanges)

This will allow to use arrays of any origin. The methods in the chain must also accept ReadOnlySpan<T>.

API Usage

DataTable dt = new
DataTable
();

dt.Columns.Add(nameof(TVP.Property1), typeof(int));
dt.Columns.Add(nameof(TVP.Property2), typeof(int));
dt.Columns.Add(nameof(TVP.Property3), typeof(int));

ReadOnlySpan<object> values = ArrayPool<object>.Shared.Rent(10).AsSpan().Slice(0, 3);

dt.LoadDataRow(values, false);

Alternative Designs

Or simply skip validating the length of the input array (as described above) and instead base logic on the number of columns.

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.DatauntriagedNew issue has not been triaged by the area owner

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions