1

I am trying to get the user's last login date and if no login records exist, return today's date. I am need some help getting there. I am getting the following error and do not even have the default value component.

Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.DateTime?'

DateTime? logdate = from userlog in
                                    (from userlog in db.UserLogEntities
                                     where
                                       userlog.UserID == 1 &&
                                       userlog.Action == "Logon" &&
                                       userlog.ActionTag == "Success"
                                     select new
                                     {
                                         userlog.LogDate,
                                         Dummy = "x"
                                     })
                                group userlog by new { userlog.Dummy } into g
                                select new
                                {
                                    Column1 = (DateTime?)g.Max(p => p.LogDate)
                                };

3 Answers 3

8

Below is what I came up with. Let me know if there is a better way.

LastLogin = (from ul in db.UserLogEntities
                                 where ul.UserID == u.UserID &&
                                    ul.Action == "Logon" &&
                                    ul.ActionTag == "Success"
                                 select ul.LogDate).DefaultIfEmpty(DateTime.Now).Max()
1
  • Another option: ...select (DateTime?)ul.LogDate).DefaultIfEmpty().Max()
    – Hans
    Feb 12, 2013 at 0:27
1

That is also a doable solution. But you can also use. This solution so then you do not need an aggregate function.

var user= (
        from ul in db.UserLogEntities
         where ul.UserID == u.UserID &&
            ul.Action == "Logon" &&
            ul.ActionTag == "Success"
        orderby ul.LogDate descending
         select ul.LogDate??DateTime.Now ).FirstOrDefault();
1
  • thanks. one key is I need to populate with today's date if no records exist.
    – scottrakes
    Dec 17, 2011 at 23:08
0

This is because you have selected a list of the dates. You should use FirstOrDefault() or SingleOrDefault() at the end of your query like this

select new
                                {
                                    Column1 = (DateTime?)g.Max(p => p.LogDate)
                                }.FirstOrDefault();

Or

select new
                                    {
                                        Column1 = (DateTime?)g.Max(p => p.LogDate)
                                    }.SingleOrDefault();
2
  • Similar error: 'AnonymousType#1' does not contain a definition for 'FirstOrDefault' and no extension method 'FirstOrDefault' accepting a first argument of type 'AnonymousType#1'...
    – scottrakes
    Dec 17, 2011 at 13:15
  • Yeah, as mentioned this has a bug, and I don't really get the intent.
    – Hans
    Feb 12, 2013 at 0:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.