Linq to Sql Insert is complicating my life RRS feed

  • Question

  • Hi all,

    What Im trying to do  seems very simple but I cant get around it using linq to sql. Im importing data from access, excel and flat files to an sql database. I have an entity say Address. I use a long loop to go through rows from my source. I want to check if Address exists and if not create a new one and save it. Address has ID (Primary Key Auto Generated), and No (Unique indexed) among other fields.

    Trouble comes here: In my loop we may have several rows with the same No. I look for Address with same No and if it doesnt exist, it should be created. It it exists it should be updated with current row values (when user imports same file but has changed other fields). I allways get 'cannot insert duplicate' every time I try this. I have used other ORMs which look for objects created even in memory but not saved yet and they do this very effectively. I now want to use linq to sql but it doesnt seem to work for me. I also want to write minimum code for this because it involves alot of objects in the loop.

    How can I achieve this in linq to sql. Help me please before I give up!!

    Thanks and regards

    • Moved by Bin-ze Zhao Monday, August 3, 2009 10:47 AM off topic (From:Visual C# General)
    Wednesday, July 29, 2009 7:28 PM


  • You may want to consider posting to the Linq to SQL forums:


    Hope this helps.

    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Marked as answer by bughata Monday, December 20, 2010 9:33 AM
    Wednesday, July 29, 2009 7:47 PM