Search code examples
c#fo-dicom

How can i add multiple rows in DICOM dataset?


How can i add multiple rows in DICOM dataset? Without using another DICOM dataset like List<DicomDataset>?

dt = dac.ExecuteDataSet(dbCommand).Tables[0];
if (dt.Rows.Count > 0)
{
  foreach (DataRow dr in dt.Rows)
  {
    DicomDataset _dataset = new DicomDataset();
    _dataset.Add(DicomTag.SOPClassUID, SOPClassUID);
    _dataset.Add(DicomTag.SOPInstanceUID, GenerateUid());

    _dataset.Add(DicomTag.PatientID, dr["PatientID"].ToString());
    _dataset.Add(DicomTag.PatientName, dr["PatientName"].ToString());
    _dataset.Add(DicomTag.PatientBirthDate, dr["DOB"].ToString());
    _dataset.Add(DicomTag.PatientSex, dr["Sex"].ToString());

    _dataset.Add(DicomTag.AccessionNumber, dr["AccessionNumber"].ToString());
    _dataset.Add(DicomTag.RequestedProcedureDescription, dr["Procedure_Description"].
        ToString());
    _dataset.Add(DicomTag.RequestedProcedureID, dr["RequestedProcedureId"].ToString());
    _dataset.Add(DicomTag.Modality, dr["modality"].ToString());                           
  }
}

Solution

  • Looking at the internal structure of DicomDataset we can see that when you add multiple items, the following methods are called

    public DicomDataset Add<T>(DicomTag tag, params T[] values)
    {
        return DoAdd(tag, values, false);
    }
    
    private DicomDataset DoAdd<T>(DicomTag tag, IList<T> values, bool allowUpdate)
    {
        [...]
        return DoAdd(vr, tag, values, allowUpdate);
    }
    

    which eventually results in a call like this

    if (typeof(T) == typeof(string)) 
        return DoAdd(new DicomApplicationEntity(tag, values.Cast<string>().ToArray()), allowUpdate);
    

    Which adds DicomApplicationEntity objects with the arrays you are passing. Anyway, when you are trying to add multiple objects with the same tag it will fail, since the internal dictionary can only hold one object per type. Hence I guess, that you can add multiple rows for one tag with

    _dataset.Add(DicomTag.PatientID, dt.Rows.OfType<DataRow>().Select(row => row["PatientID"].ToString()).ToArray());