IAmGroot
IAmGroot

Reputation: 13855

ASP.NET MVC3: Applying a SELECT / WHERE Statement from MVC View

I have list of items contained in viewbag ViewBag.RoomBookings

As an example:

I want all the Roombookings with ViewBag.RoomBookings.RoomNo = 6 AND ViewBag.RoomBookings.Time < DateTime.Now AND ViewBag.RoomBookings.Time < DateTime.Now + 3600

Looping through is a NO.

And it must be done on the view, as I also need access to all the other roombookings too to populate a timetable.

Alternatively, Im thinking of Hash lists? Where I could list it with RoomNo, and Time and access it that way. But I cant find any good documentation on this if its even possible.

Ive tried a few things (just to test a method that works (these wont follow the same critera as above)):

      var RoomBookingsTemp = ViewBag.RoomBookings;

      var newlist = (from RoomOcc in RoomBookingsTemp where RoomOcc.EndDateTime < DateTime.Now select RoomOcc.RoomNo);
      var bookings = RoomBookingsTemp.Where(roombooking => DateCheckStart < roombooking.EndDateTime && DateCheckEnd > roombooking.StartDateTime && roombooking.RoomNo == RoomNo);
      var newlist = RoomBookingsTemp.Select(m => m["RentalNo"]); 

But none are valid.

Upvotes: 0

Views: 2815

Answers (2)

BZink
BZink

Reputation: 7947

You need to cast the RoomBookingsTemp variable. When you declare it directly from the ViewBag it's still a dynamic type [details about viewbag] and the error your seeing is that linq can't query/iterate over a dynamic type.

I'm not sure what type you're actually using but try something like this...

var RoomBookingsTemp = ViewBag.RoomBookings as List<RoomBooking>;

or

List<RoomBooking> RoomBookingsTemp = ViewBag.RoomBookings;

Upvotes: 2

James Hull
James Hull

Reputation: 3689

What's wrong with your second method? You are just missing the select.

// Some statements removed from where for clarity
var bookings = RoomBookingsTemp.Where(rb => rb.RoomNo == 6).Select(rb => rb);

Upvotes: 0

Related Questions