/// <summary>
        /// Returns the value from a field in a row of the specified table.
        /// The row is identified by the primary key field name and value that is passed along.
        /// The table is identified by the provided table name.
        /// This method can be used to retrieve information from 1:1 related secondary tables
        /// in scenarios where child items have related table that extend the child row's schema.
        /// In most scenarios, this overload is NOT needed. Use the simpler overloads instead!
        /// </summary>
        /// <typeparam name="TField">The type of the field.</typeparam>
        /// <param name="fieldName">Name of the field that contains the value.</param>
        /// <param name="ignoreNulls">Should nulls be returned (true) or should default values be provided for nulls (false)?</param>
        /// <param name="tableName">Name of the table that contains the field</param>
        /// <param name="searchExpression">Search (filter) expression used to identify the record in the secondary table</param>
        /// <returns>Value object</returns>
        /// <example>GetFieldValue("CustomerStatus", true, "ExtendedCustomerInformationTable", "cust_id = 'x'");</example>
        /// <remarks>May throw ArgumentException and RowNotInTableException.</remarks>
        protected virtual TField ReadFieldValue <TField>(string fieldName, bool ignoreNulls, string tableName, string searchExpression)
        {
            // TODO: We may have to do something with the search expression, which should also support maps

            var entity = ParentEntity as BusinessEntity;

            if (entity == null)
            {
                throw new NullReferenceException("Parent entity is not a business entity.");
            }

            fieldName = ((BusinessEntity)ParentEntity).GetInternalFieldName(fieldName, tableName);

            var secondaryTable = CurrentRow.Table.DataSet.Tables[tableName];

            // Now that we have the table, we can try to find the desired row
            var foundRows = secondaryTable.Select(searchExpression);

            // We expect to find exactly one row. If fewer or more rows get returned, we throw an error
            if (foundRows.Length != 1)
            {
                if (foundRows.Length > 1)
                {
                    // The record was not uniquely identified
                    throw new ArgumentException("Unable to find unique record: " + foundRows.Length.ToString(NumberFormatInfo.InvariantInfo) + " records returned by search expression '@searchExpression'", "searchExpression");
                }
                throw new RowNotInTableException("Search record not found by expression '@searchExpression'");
            }

            // We did find the row. We can now make sure it has the field we desire
            CheckColumn(fieldName, secondaryTable);

            // Finally, we are ready to retrieve the desired field
            return(BusinessEntityHelper.GetFieldValue <TField>(entity, foundRows[0].Table.DataSet, tableName, fieldName, foundRows[0], ignoreNulls));
        }
 /// <summary>
 /// Returns the value of the specified field in the database
 /// </summary>
 /// <typeparam name="TField">The type of the field.</typeparam>
 /// <param name="fieldName">Field name</param>
 /// <param name="ignoreNulls">Should nulls be ignored and returned as such (true) or should the be turned into default values (false)?</param>
 /// <param name="currentRow">Current data row, which contains the value we are interested in</param>
 /// <returns>Value object</returns>
 protected virtual TField ReadFieldValue <TField>(string fieldName, bool ignoreNulls, DataRow currentRow)
 {
     if (!(ParentEntity is BusinessEntity entity))
     {
         throw new NullReferenceException("Parent entity is not a business entity.");
     }
     return(BusinessEntityHelper.GetFieldValue <TField>(entity, currentRow.Table.DataSet, TableName, fieldName, currentRow, ignoreNulls));
 }
 /// <summary>
 /// Returns the value of the specified field in the database
 /// </summary>
 /// <param name="fieldName">Field name</param>
 /// <param name="ignoreNulls">Should nulls be ignored and returned as such (true) or should the be turned into default values (false)?</param>
 /// <returns>Value object</returns>
 protected virtual object GetFieldValue(string fieldName, bool ignoreNulls = false)
 {
     if (!(ParentEntity is BusinessEntity entity))
     {
         throw new NullReferenceException("Parent entity is not a business entity.");
     }
     return(BusinessEntityHelper.GetFieldValue <object>(entity, CurrentRow.Table.DataSet, TableName, fieldName, CurrentRow, ignoreNulls));
 }
        /// <summary>
        ///     Returns the value of the specified field in the database
        /// </summary>
        /// <typeparam name="TField">The type of the field.</typeparam>
        /// <param name="fieldName">Field name</param>
        /// <param name="mode">Accessing a field in the parent (link) or target (child/foreign) table?</param>
        /// <param name="ignoreNulls">
        ///     Should nulls be ignored and returned as such (true) or should the be turned into default
        ///     values (false)?
        /// </param>
        /// <returns>Value object</returns>
        protected virtual TField ReadFieldValue <TField>(string fieldName, XLinkItemAccessMode mode, bool ignoreNulls = false)
        {
            if (mode == XLinkItemAccessMode.CurrentTable)
            {
                return(ReadFieldValue <TField>(fieldName));
            }

            if (!(ParentEntity is BusinessEntity entity))
            {
                throw new NullReferenceException("Parent entity is not a business entity.");
            }
            return(BusinessEntityHelper.GetFieldValue <TField>(entity, CurrentTargetRow.Table.DataSet, CurrentTargetRow.Table.TableName, fieldName, CurrentTargetRow, ignoreNulls));
        }
        /// <summary>
        ///     Returns the value of the specified field in the database
        /// </summary>
        /// <param name="fieldName">Field name</param>
        /// <param name="mode">Accessing a field in the parent (link) or target (child/foreign) table?</param>
        /// <param name="ignoreNulls">
        ///     Should nulls be ignored and returned as such (true) or should the be turned into default
        ///     values (false)?
        /// </param>
        /// <returns>Value object</returns>
        protected virtual object GetFieldValue(string fieldName, XLinkItemAccessMode mode, bool ignoreNulls = false)
        {
            if (mode == XLinkItemAccessMode.CurrentTable)
            {
                return(GetFieldValue(fieldName));
            }

            var entity = ParentEntity as BusinessEntity;

            if (entity == null)
            {
                throw new NullReferenceException("Parent entity is not a business entity.");
            }
            return(BusinessEntityHelper.GetFieldValue <object>(entity, CurrentTargetRow.Table.DataSet, CurrentTargetRow.Table.TableName, fieldName, CurrentTargetRow, ignoreNulls));
        }