Question: On this Function I get an Invalid Cast Exception with this as the Detailed error

> 
> Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery`1[<>f__AnonymousType8`2[System.Nullable`1[System.TimeSpan],System.String]]' to type 'System.Linq.IOrderedQueryable`1[YourReservation.Models.Reservation]'.
> 
> 
> 

Code:

```
[HttpGet]
[Route("api/Reservations/getPartyNames")]
public IOrderedQueryable<Reservation>getPartyNames()
{
    var query = (from r in db.Reservations
                 where r.ReservationDate == DateTime.Now
                 select new { Time = r.ReservationTime, FullName = r.FirstName + " " + r.LastName });

    return (IOrderedQueryable<Reservation>)query;
}

```

I don't exactly see what is wrong because I have made this particular cast on a return variable numerous times before.

Answer: 
You can see in your code, that List is an ArrayList. The method in ArrayList called *iterator()* will return an implementation of Iterator. This implementation of Iterator will have the method *hasNext()* implemented.

It's great to be curious, and as the comment said, you can click into the code in an IDE (or if that doesn't work for you, set a break point in a debugger and step in). Reading the Java code is a great way to de-mystify it.

Of course, you can generally can expect the Java people to know what they are doing, and trust the implementations you get back to be sensible, and fit for general use. The caveat here is that you should also read the documentation. eg. from <https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html>

> 
> The iterators returned by this class's iterator and listIterator
>  methods are fail-fast: if the list is structurally modified at any
>  time after the iterator is created, in any way except through the
>  iterator's own remove or add methods, the iterator will throw a
>  ConcurrentModificationException. Thus, in the face of concurrent
>  modification, the iterator fails quickly and cleanly, rather than
>  risking arbitrary, non-deterministic behavior at an undetermined time
>  in the future.
> 
> 
>
The `next()` and `hasNext()` methods are part of the `Iterator` interface. Each specific implementation will provide their own versions. You can take a look at how they are defined for the `ArrayListIterator` in the commons-collection library [here](http://grepcode.com/file_/repo1.maven.org/maven2/commons-collections/commons-collections/3.2.1/org/apache/commons/collections/iterators/ArrayListIterator.java/?v=source) or `SingletonListIterator` [here](http://grepcode.com/file_/repo1.maven.org/maven2/commons-collections/commons-collections/3.2.1/org/apache/commons/collections/iterators/SingletonListIterator.java/?v=source).