update query in linq to sql

update query in linq to sql

Hi Everyone,

I'm going to share the code sample for update query in linq to sql in c# asp.net

Today, I have a requirement to update the log details in the DB with respect to the UserId.


 /// <summary>
 /// Update to user device access log table using linq to sql in c# .net

 /// </summary>
 using (ExceptionLogDataContext dbContext = new ExceptionLogDataContext())
 {           
     if (!string.IsNullOrEmpty(UserId))
     {           
        var IsExistedUser = (from user in dbContext.UserDeviceAccessLogs
              where user.UserId == Convert.ToInt32(UserId)
              select user).FirstOrDefault();

        if (IsExistedUser.UserId != null)
        {
                  IsExistedUser.CreatedDate = DateTime.UtcNow;
                  IsExistedUser.DeviceType = DeviceType;
                  IsExistedUser.LastAccessed = DateTime.UtcNow;
                  IsExistedUser.LatestAccessIP = AccessIPAddress; 
               
                  dbContext.SubmitChanges();
         }
     }

 }



ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.
^