Abhishek
Abhishek

Reputation: 173

"ERROR: Only one expression can be specified in the select list" Linq To Sql

var loggedInHours = db.LoginLogs.Where(l => l.UserId == u.Id && l.UserSessionStop != null)
                                                                     .Sum(ls=> ls.UserSessionStart.Subtract(ls.UserSessionStop.Value).Hours)

I am trying to calculate Total LoggedIn Hours using this linq query.. But its giving me this error "Only one expression can be specified in the select list when the subquery is not introduced with EXISTS." I don't know whats wrong with it..plz help

Upvotes: 0

Views: 361

Answers (1)

mshsayem
mshsayem

Reputation: 18018

Try if this works:

var loggedInHours = db.LoginLogs.Where(l => l.UserId == u.Id && l.UserSessionStop != null)
                      .Select(l=> new { 
                                         StartTime = l.UserSessionStart,  
                                         EndTime = l.UserSessionStop
                                      })
                     .ToList()
                     .Sum(c=> c.StartTime - c.EndTime);

btw, Is UserSessionStop nullable? If yes, then what will be the value to be subtracted?

Upvotes: 2

Related Questions