Unable to pass an object of type WhereSelectListIterator

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"
                }

                //Continued on with the rest of states

            }.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
        {
            //Return a list of options for dropdown list
            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;  //<=== Error 
        }

, GetStates.

, , , .

+5
2

LINQ , , , , . 2 . GetStateList:

public class PropertyRepository: IPropertyRepository
{
    public List<States> GetStates() 
    {
        return States.GetStates().ToList();
    }
}

:

[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 });
}
+3

:

var stateList = RawStates.Select(c => new { DisplayText = c.StateName, 
                                            Value = c.StateID.ToString() });
return (List<States>)stateList;

:

  • Select List<T>
  • States ;

ToList(); Select, . , , , States DisplayText Value.

, GetStates - GetStatesList(), , .

, , , , .

+6

All Articles