0

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.

6
  • i would think the linq approach you have suggested would do what you are trying to do. can you state what it is that makes you say it's not valid? Commented Nov 24, 2011 at 14:03
  • Works fine in the controller but not the view. Commented Nov 24, 2011 at 14:32
  • if linq is the first one, "Query expressions over source type 'dynamic' are not allowed". Other 2 give "Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type " Commented Nov 24, 2011 at 14:35
  • 1
    Quoting some worrying phrases from your question: I have list of items contained in viewbag and And it must be done on the view. Using ViewData/ViewBag, and writing complex code in the view which would otherwise belong to a view model are two very bad practices for me. Commented Nov 24, 2011 at 15:02
  • The view is for presentation and shouldn't contain logic. Create the structure you need in the controller and display it in the view Commented Nov 24, 2011 at 15:07

2 Answers 2

2

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;
Sign up to request clarification or add additional context in comments.

1 Comment

scratches head I swear I tried this. On second thoughts,maybe I left out the fact its a list of custom objects. Cheers!
0

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);

4 Comments

"Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type "
Can you show the code where you declare RoomBookingsTemp from the ViewBag.
var RoomBookingsTemp = ViewBag.RoomBookings
It's because ViewBag is dynamic. Either use a statically typed view model (which I highly recommend) or try: List<RoomBookings> RoomBookingsTemp = ViewBag.RoomBookings; // Assuming ViewBag.RoomBookings is a List of RoomBookings

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.