views:

8

answers:

0

Hi, I got mad about this DataTable and merging behavior.
Today I experienced this: I have a datatable with 4 keys and 3 additional data columns. Now I need to merge a second table with again 4 keys (same as first datatable – name and type identical) but 6 data columns not hold to the first datatable. The result after merge should contain the data of the second table as new rows. But all the sudden DataTable.Merge collapses the data of the second datatable to one line, i.e. dependent on preserve changes the first or the last row will be added.
Merge seams to ignore some key?

Does anybody experienced the same? Can this problem be solved in another way then merge row wise or to use own (managed) code? Both ways are very slow for large datatables.

-- here the output (and the code generating the output) of a test

Keys of: destination
ID Location p1 p2
System.Int64 System.String System.Int32 System.Int32
690 C31 757602046 757602046
690 C32 757602046 757602046
Keys of: second - to be merged
ID Location PositionX p1 p2
System.Int64 System.String System.Int32 System.Int32
690 E1 -340479406 15554346
690 E1 15685418 15554346
690 E1 1178484832 15554346
690 E1 -1953683050 15554346
690 E1 -790883636 15554346
690 E1 -340544942 15554346
690 E1 15619882 15554346
690 E1 1178419296 15554346
690 E1 -1953748586 15554346
690 E1 -790949172 15554346
690 E1 -340610478 15554346
Keys of: destination - merged
ID Location p1 p2
System.Int64 System.String System.Int32 System.Int32
690 C31 757602046 757602046
690 C32 757602046 757602046
690 E1 -340479406 15554346

public static void DebugWriteKeys(this DataTable tab, string name)
    {
        Debug.WriteLine("Keys of: " + name);
        Debug.WriteLine(tab.PrimaryKey.Select(l => l.ColumnName).Aggregate((m, l) => m + "\t" + l));
        Debug.WriteLine(tab.PrimaryKey.Select(l => l.DataType.ToString()).Aggregate((m, l) => m + "\t" + l));
        foreach (DataRow r in tab.Rows)
            Debug.WriteLine(tab.PrimaryKey.Select(l => r[l].ToString()).Aggregate((m, l) => m + "\t" + l));
    }

I did some drilldown of the error. finally it turns out like this: When I try to merge the second to itself it fails because of violated constrains. And I found this when I try to merge only the frist row of the second table to the second table:

dest.Rows[0]["p1"] == dest.Rows[10]["p1"]
false

(int)dest.Rows[0]["p1"] == (int)dest.Rows[10]["p1"]
true

  • this is not really what I'm expecting ...