Default Values (of C# variables) Issue in LINQ to SQL Update

后端 未结 2 1094
刺人心
刺人心 2020-12-11 13:04

I have the following code for updating Account table with LINQ to SQL. AccountNumber is the primary key column. The only value which need to be updated is AccountType; howev

2条回答
  •  难免孤独
    2020-12-11 13:22

    This should do:

    int number = 4;
    var acc1 = new accountRepository.Accounts.Where(a => a.Number == number).FirstOrDefault();
    
    if (acc1 == null)
    {
        // Not found by ID, create new
        acc1 = new RepositoryLayer.Account();
        acc1.Number = number;
        accountRepository.Accounts.AddObject(acc1);
    }
    
    acc1.AccountType = "Verify";
    
    accountRepository.SubmitChanges();
    

提交回复
热议问题