Click here to Skip to main content
15,890,399 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using C# NHibernate to create a dashboard and what I am trying to do is group the records by timestamp so if there are identical timestamps it will only show once. I have tried Last(t => t.timestamp) but receive the error of cannot convert type System.DateTime to bool and when I tried .GroupBy(t => t.timestamp) I get <IGrouping<DateTime, Answer> does not contain a definition for my fields of eventId, userId, and timestamp.



Open to suggestions. Thanks

What I have tried:

public ActionResult Dashboard()
        {
            using (ISession session = OpenNHibernateSession.OpenSession())
            {

               /*These answers gets my list of data. Here is where I am trying 
                 to use a GroupBy clause so records with identical timestamps
                 will only be shown once.*/
                var answers = session.Query<Answer>().OrderByDescending(t => t.timestamp).ToList();
               
                var dashboardVms = new List<DashboardViewModel>();
                foreach (var answer in answers)
                {
                    var dashboardVm = new DashboardViewModel
                    {
                        EventName = session.Get<Event>(answer.eventId, LockMode.Read).name,
                        EventId = answer.eventId,
                        UserId = answer.userId,
                        TimeStamp = answer.timestamp
                    
                    };
                    dashboardVms.Add(dashboardVm);
                }
                
                return View(dashboardVms);
            }
             
        }
Posted
Updated 6-Jul-18 10:28am

1 solution

I think you can do something like this:
session
    .Query<Answer>()
    .OrderByDescending(t => t.timestamp)
    .GroupBy(x => x.timestamp)
    .Select(x => new
    {
        EventName = session.Get<Event>(x.eventId, LockMode.Read).name,
        EventId = x.eventId,
        UserId = x.userId,
        TimeStamp = x.Key
    }).ToList();

You might want to chain an AsEnumerable call between the GroupBy and the select if the Select call in not compatible with your provider.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900