Linq, should I join those two queries together?

Posted by 5YrsLaterDBA on Stack Overflow See other posts from Stack Overflow or by 5YrsLaterDBA
Published on 2010-05-11T19:00:30Z Indexed on 2010/05/11 19:04 UTC
Read the original article Hit count: 206

Filed under:
|

I have a Logins table which records when user is login, logout or loginFailed and its timestamp. Now I want to get the list of loginFailed after last login and the loginFailed happened within 24 hrs.

What I am doing now is get the last login timestamp first. then use second query to get the final list. do you think I should join those two queris together? why not? why yes?

                    var lastLoginTime = (from inRecord in db.Logins
                                  where inRecord.Users.UserId == userId
                                      && inRecord.Action == "I"
                                  orderby inRecord.Timestamp descending
                                         select inRecord.Timestamp).Take(1);
                    if (lastLoginTime.Count() == 1)
                    {
                        DateTime lastInTime = (DateTime)lastLoginTime.First();

                        DateTime since = DateTime.Now.AddHours(-24);
                        String actionStr = "F";    
                        var records = from record in db.Logins
                                      where record.Users.UserId == userId
                                          && record.Timestamp >= since
                                          && record.Action == actionStr
                                          && record.Timestamp > lastInTime
                                      orderby record.Timestamp 
                                      select record;
                    }

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ