locked
Get Result from GroupBy with Count() RRS feed

  • Question

  • User-259260637 posted

    I have been trying to get the result from a query that looks like it is correct in the Output window but I can't seem to get the value, I always end up with the linq query,

    var count = db.tblMemberBookings.Where(x => x.CourseDateId == courseId)
                        .GroupBy(x => new { x.NumberOfPlacesBooked, x.CourseDateId })
                        .Select(g => new { g.Key.NumberOfPlacesBooked, g.Key.CourseDateId, g = g.Count() }).FirstOrDefault();

    courseId is a Guid and Number of Places is an Int. 

    This is what I get

    count	{ NumberOfPlacesBooked = 1, CourseDateId = {4b0bc8cb-5adf-4943-9fa9-290a20d7f705}, g = 1 }	<Anonymous Type>
    

    How would I get the places booked into a variable that i could them query to know how many places have been booked and show the correct amount of places remaining. 

    Say course has 12 places - numberOfPlacesBooked

    var count = db.tblMemberBookings.Where(x => x.CourseDateId == courseId)
                        .GroupBy(x => new { x.NumberOfPlacesBooked, x.CourseDateId })
    .Select(g => new { g.Key.NumberOfPlacesBooked, g.Key.CourseDateId }).Count();



    I solved it !!
    Monday, September 28, 2015 11:11 AM

Answers

  • User-271186128 posted

    Hi StudentRik,

    Glad to see you have solved your problem by yourself, if you have any other questions, please feel free to post in this forum.

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 29, 2015 2:34 AM