Unable to cast object of type WhereSelectListIterator
Solution 1
You are projecting your LINQ query to an anonymmous object and not to a State list which obviously cannot work. The 2 types are incompatible. So start by modifying your repository layer and get rid of the GetStateList
method:
public class PropertyRepository: IPropertyRepository
{
public List<States> GetStates()
{
return States.GetStates().ToList();
}
}
and then project to the desired structure in your controller:
[HttpPost]
public JsonResult GetStateOptions()
{
var states = propertyRepository.GetStateList();
var options = states.Select(x => new
{
DisplayText = c.StateName,
Value = c.StateID.ToString()
}).ToList();
return Json(new { Result = "OK", options = states });
}
Solution 2
This is the problem:
var stateList = RawStates.Select(c => new { DisplayText = c.StateName,
Value = c.StateID.ToString() });
return (List<States>)stateList;
Two issues:
Select
doesn't return aList<T>
- You're not= selecting
States
objects; you're selecting an anonymous type
The first is fixable using ToList()
; the second is fixable either by changing your Select
call or by changing your method's return type. It's not really clear what you really want to return, given that States
doesn't have a DisplayText
or Value
property.
I would expect a method of GetStates
to return the states - in which case you've already got GetStatesList()
which presumably does what you want already.
Basically, you need to think about the type you really want to return, and make both your method return type and the method body match that.
Shawn
Updated on June 22, 2022Comments
-
Shawn almost 2 years
I have been attempting to figure out why a Linq query that returns a list of U.S. States formatted for a drop down list will not cast to a List when the code returns to the calling method. The error that I get is:
Unable to cast object of type 'WhereSelectListIterator'2[StateListing.States,<>f__AnonymousTypea'2[System.String,System.String]]' to type 'System.Collections.Generic.List`1[StateListing.States]'
The namespace StateListing from the error, is a dll library that has a class called States returning an IEnumerable List of states shown below.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace StateListing { public class States { public string StateAbbriviation { get; set; } public int StateID { get; set; } public string StateName { get; set; } static int cnt = 0; public static IEnumerable<States> GetStates() { return new List<States> { new States { StateAbbriviation = "AL", StateID=cnt++, StateName = "Alabama" }, new States { StateAbbriviation = "AL", StateID=cnt++, StateName = "Alaska" } //Continued on with the rest of states }.AsQueryable(); } } }
In my control I make a call to GetStates that returns a List of States from the class library above.
[HttpPost] public JsonResult GetStateOptions() { try { //Return a list of options for dropdown list var states = propertyRepository.GetStates(); return Json(new { Result = "OK", options = states }); }
In the property repository class I have two methods one to get the StateList from the library, and another to format the listing of states for a drop down list in my view.
public List<States> GetStateList() { var items = (from s in States.GetStates() select s).ToList(); return items; } List<States> IPropertyRepository.GetStates() { try { List<States> RawStates = GetStateList(); var stateList = RawStates.Select(c => new { DisplayText = c.StateName, Value = c.StateID.ToString() }); return (List<States>)stateList; //<=== Error }
The error occurs when the code reaches the return within the GetStates method.
Any help with this casting problem explaining what I'm doing wrong would be appreciated.
-
Shawn about 11 yearsThank Darin that worked. I guess I was trying to make it more complicated than it needed to be.
-
Shawn about 11 yearsThanks Jon for your explanation it was helpful. To answer the question of why I did not have a DisplayText, and Value property in the state class was because they are being passed into a Jtable plugin that needs to have these properties assigned in the query for a dropdown list of "options".
-
Jon Skeet about 11 years@Shawn: But do you understand how your method declaring that it would return a
List<States>
is incompatible with this?