Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to cast object of type WhereSelectListIterator

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.

like image 560
Shawn Avatar asked Mar 03 '13 09:03

Shawn


2 Answers

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 a List<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.

like image 145
Jon Skeet Avatar answered Nov 15 '22 09:11

Jon Skeet


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 });
}
like image 28
Darin Dimitrov Avatar answered Nov 15 '22 09:11

Darin Dimitrov