troubleshooting Question

Linq to SQL

Avatar of Robb Hill
Robb HillFlag for United States of America asked on
C#.NET ProgrammingLINQ QueryASP.NET
1 Comment1 Solution766 ViewsLast Modified:
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 )




Lokesh B R

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Top Expert 2015

The Distinguished Expert awards are presented to the top veteran and rookie experts to earn the most points in the top 50 topics.

Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros