Class DataRow

Namespace
RentADeveloper.DbConnectionPlus.Dynamic
Assembly
RentADeveloper.DbConnectionPlus.dll

The data of a single row returned by an SQL query.

public class DataRow : IDictionary<string, object?>, ICollection<KeyValuePair<string, object?>>, IEnumerable<KeyValuePair<string, object?>>, IEnumerable, IDynamicMetaObjectProvider
Inheritance
DataRow
Implements
Inherited Members

Examples

// Works everywhere, including Native AOT.
var id = row["Id"];

// Additionally available where run-time code generation is supported.
dynamic product = row;
var name = product.Name;

Remarks

Columns are always available through the string indexer — row["Id"]. This is the form of access to use in an application published with Native AOT, and it needs no cast.

Where the runtime supports dynamic code generation, a row can additionally be assigned to a dynamic variable to read and write its columns as members — row.Id. Member access is bound by the Dynamic Language Runtime and behaves exactly like the indexer, including throwing KeyNotFoundException for a column the row does not contain. It is not available in an application published with Native AOT, where the compiler reports the dynamic usage at the call site that wrote it.

Through a dynamic reference, reading or writing a property always addresses a column: row.Count reads the column named Count and throws KeyNotFoundException if the row has no such column — it does not read Count. Calling a method still resolves against this type, so row.ContainsKey("Id") and row.ToString() behave normally. Use a statically typed DataRow reference to reach Count, Keys and Values.

Constructors

DataRow(IDictionary<string, object?>)

The data of a single row returned by an SQL query.

public DataRow(IDictionary<string, object?> columns)

Parameters

columns IDictionary<string, object>

The columns of the data row. The keys are expected to be the column names, and the values are expected to be the corresponding column values.

Examples

// Works everywhere, including Native AOT.
var id = row["Id"];

// Additionally available where run-time code generation is supported.
dynamic product = row;
var name = product.Name;

Remarks

Columns are always available through the string indexer — row["Id"]. This is the form of access to use in an application published with Native AOT, and it needs no cast.

Where the runtime supports dynamic code generation, a row can additionally be assigned to a dynamic variable to read and write its columns as members — row.Id. Member access is bound by the Dynamic Language Runtime and behaves exactly like the indexer, including throwing KeyNotFoundException for a column the row does not contain. It is not available in an application published with Native AOT, where the compiler reports the dynamic usage at the call site that wrote it.

Through a dynamic reference, reading or writing a property always addresses a column: row.Count reads the column named Count and throws KeyNotFoundException if the row has no such column — it does not read Count. Calling a method still resolves against this type, so row.ContainsKey("Id") and row.ToString() behave normally. Use a statically typed DataRow reference to reach Count, Keys and Values.

Properties

Count

Gets the number of elements contained in the ICollection<T>.

public int Count { get; }

Property Value

int

The number of elements contained in the ICollection<T>.

IsReadOnly

Gets a value indicating whether the ICollection<T> is read-only.

public bool IsReadOnly { get; }

Property Value

bool

true if the ICollection<T> is read-only; otherwise, false.

this[string]

Gets or sets the element with the specified key.

public object? this[string key] { get; set; }

Parameters

key string

The key of the element to get or set.

Property Value

object

The element with the specified key.

Exceptions

ArgumentNullException

key is null.

KeyNotFoundException

The property is retrieved and key is not found.

NotSupportedException

The property is set and the IDictionary<TKey, TValue> is read-only.

Keys

Gets an ICollection<T> containing the keys of the IDictionary<TKey, TValue>.

public ICollection<string> Keys { get; }

Property Value

ICollection<string>

An ICollection<T> containing the keys of the object that implements IDictionary<TKey, TValue>.

Values

Gets an ICollection<T> containing the values in the IDictionary<TKey, TValue>.

public ICollection<object?> Values { get; }

Property Value

ICollection<object>

An ICollection<T> containing the values in the object that implements IDictionary<TKey, TValue>.

Methods

Add(KeyValuePair<string, object?>)

Adds an item to the ICollection<T>.

public void Add(KeyValuePair<string, object?> item)

Parameters

item KeyValuePair<string, object>

The object to add to the ICollection<T>.

Exceptions

NotSupportedException

The ICollection<T> is read-only.

Add(string, object?)

Adds an element with the provided key and value to the IDictionary<TKey, TValue>.

public void Add(string key, object? value)

Parameters

key string

The object to use as the key of the element to add.

value object

The object to use as the value of the element to add.

Exceptions

ArgumentNullException

key is null.

ArgumentException

An element with the same key already exists in the IDictionary<TKey, TValue>.

NotSupportedException

The IDictionary<TKey, TValue> is read-only.

Clear()

Removes all items from the ICollection<T>.

public void Clear()

Exceptions

NotSupportedException

The ICollection<T> is read-only.

Contains(KeyValuePair<string, object?>)

Determines whether the ICollection<T> contains a specific value.

public bool Contains(KeyValuePair<string, object?> item)

Parameters

item KeyValuePair<string, object>

The object to locate in the ICollection<T>.

Returns

bool

true if item is found in the ICollection<T>; otherwise, false.

ContainsKey(string)

Determines whether the IDictionary<TKey, TValue> contains an element with the specified key.

public bool ContainsKey(string key)

Parameters

key string

The key to locate in the IDictionary<TKey, TValue>.

Returns

bool

true if the IDictionary<TKey, TValue> contains an element with the key; otherwise, false.

Exceptions

ArgumentNullException

key is null.

CopyTo(KeyValuePair<string, object?>[], int)

Copies the elements of the ICollection<T> to an Array, starting at a particular Array index.

public void CopyTo(KeyValuePair<string, object?>[] array, int arrayIndex)

Parameters

array KeyValuePair<string, object>[]

The one-dimensional Array that is the destination of the elements copied from ICollection<T>. The Array must have zero-based indexing.

arrayIndex int

The zero-based index in array at which copying begins.

Exceptions

ArgumentNullException

array is null.

ArgumentOutOfRangeException

arrayIndex is less than 0.

ArgumentException

The number of elements in the source ICollection<T> is greater than the available space from arrayIndex to the end of the destination array.

GetEnumerator()

Returns an enumerator that iterates through the collection.

public IEnumerator<KeyValuePair<string, object?>> GetEnumerator()

Returns

IEnumerator<KeyValuePair<string, object>>

An enumerator that can be used to iterate through the collection.

GetMetaObject(Expression)

Returns the DynamicMetaObject that binds member access on this row to its columns.

protected virtual DynamicMetaObject GetMetaObject(Expression parameter)

Parameters

parameter Expression

The expression representing this row at the dynamic call site.

Returns

DynamicMetaObject

The DynamicMetaObject that binds member access on this row to its columns.

Remarks

This is Dynamic Language Runtime plumbing that supports row.ColumnName on a dynamic reference. It is not meant to be called directly. Override it to change how member access on a derived row is bound.

Remove(KeyValuePair<string, object?>)

Removes the first occurrence of a specific object from the ICollection<T>.

public bool Remove(KeyValuePair<string, object?> item)

Parameters

item KeyValuePair<string, object>

The object to remove from the ICollection<T>.

Returns

bool

true if item was successfully removed from the ICollection<T>; otherwise, false. This method also returns false if item is not found in the original ICollection<T>.

Exceptions

NotSupportedException

The ICollection<T> is read-only.

Remove(string)

Removes the element with the specified key from the IDictionary<TKey, TValue>.

public bool Remove(string key)

Parameters

key string

The key of the element to remove.

Returns

bool

true if the element is successfully removed; otherwise, false. This method also returns false if key was not found in the original IDictionary<TKey, TValue>.

Exceptions

ArgumentNullException

key is null.

NotSupportedException

The IDictionary<TKey, TValue> is read-only.

TryGetValue(string, out object?)

Gets the value associated with the specified key.

public bool TryGetValue(string key, out object? value)

Parameters

key string

The key whose value to get.

value object

When this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of the value parameter. This parameter is passed uninitialized.

Returns

bool

true if the object that implements IDictionary<TKey, TValue> contains an element with the specified key; otherwise, false.

Exceptions

ArgumentNullException

key is null.