Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I'm saving a record in my database by passing 3 parameters, the number parameter is passed to pick the number of records from one table to another table. However the loop keep inserting the same number of records instead of checking for existing stickercodes in another IntermediaryAssignment table before inserting from sticker distribution table.the foreach statement seems not to be working well expecially
Hide Copy Code
if (dbSet.Any(s => s.StickerCode != sticker.StickerCode))


Kindly help.Also it doesn't update the IntermediaryDispatched as well

What I have tried:

public async Task<bool> Save(string company, int number,string
registrationNumber)
  {

      using (var trans = _dbContext.Database.BeginTransaction())
      {
          var db = new DatabaseContext();
         var dbSet = _dbContext.Set<IntermediaryAssignment>();

          // set the database
          var check =await  (from s in db.StickerDistributions
                       join i in db.IntermediaryAssignment
                       on s.CompanyCode equals i.CompanyCode
                       where s.Dispatched == false && s.CompanyCode == company
                        && s.StickerCode != i.StickerCode
                       select s).ToListAsync();

          var datas = await (from s in db.StickerDistributions
                            where s.Dispatched == false && s.CompanyCode ==
                            company && s.IntermediaryDispatched == false
                             select s).ToListAsync();

          var data = await (from s in db.StickerDistributions
                            where s.Dispatched == false && s.CompanyCode ==
                            company &&
                            s.IntermediaryDispatched == false
                            select s).Take(number).ToListAsync();

              var intermediary = (await _repo.FindBy(s => s.RegistrationNumber
             == registrationNumber && s.Status ==
              EntityStatus.Active)).FirstOrDefault();
       foreach (var sticker in data)
          {
              if (dbSet.Any(s => s.StickerCode != sticker.StickerCode))
              {
                  var entity = new IntermediaryAssignment();

                  entity.CompanyCode = sticker.CompanyCode;
                  entity.StickerCode = sticker.StickerCode;
                  entity.RegistrationNumber = intermediary.RegistrationNumber;
                  entity.Status = EntityStatus.Active;
                  entity.CreatedDate = DateTime.Now;
                  entity.Dispatched = false;
                  entity.IntermediaryType = intermediary.IntermediaryType;
                  // entity.Sticker.Id = sticker.Sticker.Id;
                  sticker.IntermediaryDispatched = true;
                  dbSet.Add(entity);
              }

          }
          Task results =  _dbContext.SaveChangesAsync() ;
           trans.Commit();
          return  true ;
      }

  }
Posted
Updated 24-Aug-18 7:20am
Comments
ZurdoDev 24-Aug-18 13:05pm    
What part of the code does not work?
Member 13053943 24-Aug-18 14:31pm    
the foreach loop isn't working well,that is where the problem is coming from.it picks the same data stickerdistribution table and set place them in the dbSet<intermediary>

1 solution

Looks like abuse of EF.

Should be using a stored procedure and JOINs.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900