Linq to SQL

Can someone help me write out a linq query.  I am looking for the best way to optimize this.

Here is what I have so far..and what Im trying to do.  I am able to pull back a distinct record from the database by using the SessionId that is passed into this funtion and then used in the where clause.

I need to determine the following and then update based on conditions.

First I need to determine if their is a user.ConnectionId and that it is also = to Id.  If both is true then I just need to update the user.LastOn to lastOn and update the boolean field in db isActive to true.

otherwise I would also need to update the user.ConnectionId value to Id.

if var user is null I need to handle that condition as well.

Any other suggestions for good linq programming is welcome.

and anything else to make this method better formatting is welcome as well!






public class DirectModeHub : Hub
    {

        private string _userId = null;
        private string _sessionId = null;

        // Public hub methods
        public void RegisterICD(string userId, string SessionId)
        {
            _userId = userId;
            _sessionId = SessionId;

            var id = Context.ConnectionId;
            var lastOn = DateTime.Now.ToString("dd-mm-yyyy hh:MM:ss");

            using (SqlDbContext sqlDb = new SqlDbContext("SqlDbContext", null))
            {
                var user = (from u in sqlDb.UserSessions
                            where u.SessionId.ToString() == SessionId
                            select u).FirstOrDefault();

                if (user.ConnectionId != id )
                {

                    
                }

            }

        }



    }

Open in new window

LVL 12
Robb HillSenior .Net Full Stack DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lokesh B RDeveloperCommented:
Hi,

Check this

 var user = (from u in sqlDb.UserSessions
                            where u.SessionId.ToString() == SessionId
                            select u).FirstOrDefault();

if(user != null && user.ConnectionId == id)
{
    // your logic to update 
user.LastOn = your value;

}
else if(user != null)
{

     user.ConnectionId = id;
   // update here
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.