I am trying to find out why a Linq query that returns a list of US states formatted for a drop-down list will not be passed to the list when the code returns to the calling method. The error I get is:
Unable to create object of type 'WhereSelectListIterator'2 [StateListing.States, <> f__AnonymousTypea'2 [System.String, System.String]]' for input 'System.Collections.Generic.List`1 [StateListing.States]
The StateListing namespace from this error is a dll library that has a class called States that returns the 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"
}
}.AsQueryable();
}
}
}
In my control, I make a call to GetStates, which returns a list of states from the above class library.
[HttpPost]
public JsonResult GetStateOptions()
{
try
{
var states = propertyRepository.GetStates();
return Json(new { Result = "OK", options = states });
}
: StateList , - .
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;
}
, GetStates.
, , , .