none
If not in database, add to list for insertion (not working) RRS feed

  • Question

  • Topic is just like it sounds. I have a list of items that I want to put into the database, but I do not want to use an insertion if they are already there. I have three separate items where I sort out what requires an update, a deletion and an insert. The insert doesn't seem to be working so I wrote something using just standard foreach statements. Here's some code to explain the idea.
    Update:
              var iUpdateQuery = (from lTriggerFile in mTriggerFiles
                        from lRows in mDatabaseAbstractionLayer.TblFilesAwaitingComposition
                        where lRows.Filename == lTriggerFile.PERFilename
                        select new { TriggerFile = lTriggerFile, Row = lRows });
              iUpdateQuery.ToList().ForEach(x => { x.Row.Priority = x.TriggerFile.Priority; iUpdate.Add(x.Row); });
    
    Delete:
              IEnumerable<TblFilesAwaitingComposition> iDeleteQuery = (from lTriggerFile in mTriggerFiles
                                           from lRows in mDatabaseAbstractionLayer.TblFilesAwaitingComposition
                                           where lRows.Filename != lTriggerFile.PERFilename
                                           select lRows);
              iDeleteQuery.ToList().ForEach(x => { iDelete.Add(x); });
    
    Insert:
              IEnumerable<TriggerFile> iInsertQuery = (from lTriggerFile in mTriggerFiles
                                   from lRows in iUpdateQuery
                                   where lRows.TriggerFile.PERFilename != lTriggerFile.PERFilename
                                   select lTriggerFile);
              iInsertQuery.ToList().ForEach(x => { iInsert.Add(CreateDatabaseRow(x)); });
    
    And the ForEach as it's replacement.:
              foreach (TriggerFile iTrigger in mTriggerFiles)
              {
                if ((from lRows in mDatabaseAbstractionLayer.TblFilesAwaitingComposition where iTrigger.PERFilename == lRows.Filename select lRows).Count() > 0)
                {
                  continue;
                }
                else
                {
                  iInsert.Add(CreateDatabaseRow(iTrigger));
                }
              }
    
    Monday, April 18, 2011 4:17 PM

All replies

  • Two things on PerFIleName or both sides:

    1. Different case? 
    2. Unseen space?

    Do this:

     lRows.TriggerFile.PERFilename.Trim().ToLower() !=  lTriggerFile.PERFilename.Trim().ToLower()
    


    William Wegerson (www.OmegaCoder.Com)

    Friday, April 20, 2012 7:04 PM
    Moderator