public ActionResult recentbookings(string recent_bookings)
{
var recent_bookings_array = new[] { int.Parse(recent_bookings) };
booking[] bookings = db.bookings.Where(s =>
recent_bookings_array.Contains(s.Id)).ToArray();
return PartialView("_recentbookings", bookings);
}
You were using .Contains incorrectly. You are right, it is like an IN clause, but the object you invoke .Contains on should be like the IN (36525)
part of your SQL query. The above query is basically saying "Give me all of the bookings where the booking ID is in this array of recent booking id's."
Why is your result returning all booking records?
...because the Where clause argument is a Func<T, bool>
. Meaning, the result of the lambda expression must be either true or false. The expression in your question, s => recent_bookings.Contains("36525")
will always return true, since the string "36525" is contained within your recent_bookings variable. Since it is always true, all of your rows get returned. You never checked anything against your data, only your local variables.
On another note, don't call .ToList on your result unless you need to add / remove items to / from it before returning the view. .ToArray is much faster, but returns an immutable collection.