Removing Duplicate IDataRecord Fields

Ryan Peters asked on January 17, 2023 10:30

K13.0.95 I have a situation where the Columns of a DataQuery vary and cannot be explicitly set. The query joins tables with same-named columns, resulting in the error An item with the same key has already been added. due to the DataRecordContainer's constructor placing the field names of the IDataRecord into a Dictionary object.

Is there a way to get a handle on the DataRecord and remove the duplicate columns before the Query is resolved?

    public DataRecordContainer(IDataRecord dataRecord)
{
    this.dataRecord = dataRecord ?? throw new ArgumentNullException("dataRecord");
    try
    {
        for (int i = 0; i < dataRecord.FieldCount; i++)
        {
            columnNameToIndex.Add(dataRecord.GetName(i), i);
        }
    }
    catch (ArgumentException innerException)
    {
        throw new InvalidOperationException("Duplicate column name(s): " + DumpColumnNames(dataRecord), innerException);
    }
}

Correct Answer

Not Applicable answered on January 17, 2023 11:06

Before executing the DataQuery you can pre-process the query by handling the ExecuteQuery.Before event. For an example see Working with database queries in the API - Pre-processing queries in the documentation.

1 votesVote for this answer Unmark Correct answer

Recent Answers


Ryan Peters answered on January 17, 2023 16:59

Thank you Marcel, this is an excellent option. After investigating the module's arguments, having only the QueryText to manipulate vs the full MDQ, I don't believe it to be a feasible option without some hairy string manipulation.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.