Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

Select one recent row for that record with group by two columns

$
0
0

I want to select the newest record ordering by RequestDate if that has the RoomStatus as Request having the same id RoomId and RequesterId.

I have written the following query but this doesn't help to produce the output in fact this is giving me the error:

var query = _dbContext.RoomReservation
                  .Include(x => x.Room)
                  .GroupBy(x => new { x.RoomId, x.RequesterId })
                  .Select(room => new
                  {
                      Status = room.First().Status,
                      RoomId = room.First().BookId,
                      Name = room.First().Room.Name
                  }).Where(x => x.Status == Domain.Roomtatus.Requested);

The error I am getting is:

.First()' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

I mean to group by RoomIdRequesterId, order each key's item list by RequestDate descending, and pick the first.

Included Class

public class RoomReservation
{
    public int Id { get; set; }
    public RoomStatus Status { get; set; }
    public int RoomId { get; set; }
    public string RequesterId { get; set; }
    public Room Room { get; set; }
}

public class Room
{
    public int Id { get; set; }
    public string Name{ get; set; }
}

What is the correct way of Group By with multiple columns with an order by? What I am missing in my Linq query?


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>