tags:

views:

50

answers:

1

Hi,

I'm parsing a XML-file using LinqToXSD. I'm using Linq to SQL to insert the elements in the XML in corresponding tables in a database.

I'm inserting many different tables/elements in the DataContext using

DB.<tables>.InsertOnSubmit(table);

where tables could be any of about 75 different tables.

The XML is full of duplicates, and I would like to check if a given element in the XML already is added to the changeset of the DataContext.

Is there a generic way of doing this? The way i'm doing it now involves writing code for each of the different tables, which makes me think I could extract a method in some way or other and write this code just once.

To parse the XML i have a method for each element in the xml, which corresponds to a table in the database. The next two methods each handle xml-elements of different types.

 private static tbl_trackname ParseTrackName(TRACKNAMEType tRACKNAMEType)
    {
        tbl_trackname tableTrackName = new tbl_trackname();

        tableTrackName.code = tRACKNAMEType.code;
        tableTrackName.text = tRACKNAMEType.text;

        DB.tbl_tracknames.InsertOnSubmit(tableTrackName);
        return tableTrackName;
    }



 private static tbl_track ParseTrack(TRACKType tRACKType)
    {
        tbl_track tableTrack = new tbl_track();

        tableTrack.trackID = int.Parse(tRACKType.trackid);
        tableTrack.tbl_organisation = ParseOrganisation(tRACKType.organisation);
        tableTrack.tbl_trackname = ParseTrackName(tRACKType.trackname);
        tableTrack.tbl_nation = ParseNation(tRACKType.country);

        DB.tbl_tracks.InsertOnSubmit(tableTrack);
        return tableTrack;
    }

the first method inserts rows in a table named tbl_trackname, the second inserts rows in a table named tbl_track.

The missing code here is the code that checks the Datacontext if a track, or trackname is already inserted in the Datacontext. I should also check if its already in the database, but thats just a line of code so I can live with doing that for every ParseXXX() method.

A: 

Well, you could probably do something with dataContext.GetChangeSet().Inserts.OfType<Foo>()? maybe something with Any or Contains on the end?

Can you give more of an example?

Marc Gravell
I have edited the original question and addes some more information.
Beles