Linq WhereListIterator issue

AS 211 Reputation points
2021-01-12T18:51:06.203+00:00

My code is

AreaTimePlan[LocationPlanLoop - 1].AllocatedLocation = (Location)(from newLoc in Global.Locations where newLoc.ToLocations.First()== Flight.Origin && newLoc.Type.Title == AreaTimePlan[LocationPlanLoop - 1].LocationType.Title select newLoc).First();

Global.Locations is a list of Location objects
Flight.Origin is a single Location object
NewLoc.ToLocations is a list of Location objects

I get a runtime error - Unable to cast object of type 'WhereListIterator`1[Airport_Server.Location]' to type 'Airport_Server.Location'.'

My intention is that AreaTimePlan[LocationPlanLoop - 1].AllocatedLocation contains a Location which has a ToLocation equal to Origin.

Firstly I'm not sure if I need to use a cast or re-write my Linq query? Secondly whichever I need to use I would appreciate help on the correct implementation.

Thanks

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

Answer accepted by question author
  1. Timon Yang-MSFT 9,606 Reputation points
    2021-01-13T08:26:27.87+00:00

    @AS
    Did you really call First() during testing?
    The problem seems to be caused by not calling this method.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.