SolvedPrivate

Unable to cast the type 'Anonymous type' to type 'System.String'.

Posted on 2014-09-17
4
41 Views
Last Modified: 2016-02-16
Hi Expert,

I have the codes has no error on compiling but get error when execute it.

public class DepartmentController : ApiController
    {
      private DeptEntities db = new DeptEntities();
      List<string> departments = new List<string>();
      
      public IEnumerable<string> GetDept()
      {
            using (DeptEntities context = new DeptEntities())
            {
                  var result = (from Department in db.Department orderby Department.ID
                                      select new
                                      {
                                            DepartmentName = Department.Name
                                      }).Distinct();

                  if (result == null)
                        throw new HttpResponseException(HttpStatusCode.NotFound);
                  else                  
                        return result.Cast<string>().ToList();

            }

      }
}

Error:
Unable to cast the type 'Anonymous type' to type 'System.String'.
LINQ to Entities only supports casting EDM primitive or enumeration types.

Please help!
0
Comment
Question by:alexcryout
  • 2
4 Comments
 
LVL 2

Expert Comment

by:Akilandeshwari N
ID: 40329723
Add a WHERE clause to your LINQ query and exclude the NULL Departments and try.
0
 
LVL 11

Accepted Solution

by:
louisfr earned 500 total points
ID: 40330079
Your LINQ returns objects with a DepartmentName member. To return a list of strings instead, don't wrap the department name inside an object:
public IEnumerable<string> GetDept()
{
    using (DeptEntities context = new DeptEntities())
    {
        var result = (from Department in db.Department orderby Department.ID
                      select Department.Name
                     ).Distinct();

        if (result == null)
            throw new HttpResponseException(HttpStatusCode.NotFound);
        else                  
            return result.Cast<string>().ToList();
}

      }

Open in new window

0
 

Author Closing Comment

by:alexcryout
ID: 40331251
Thanks louisfr. It works!
0
 
LVL 11

Expert Comment

by:louisfr
ID: 40336016
I think you can remove all the code after the query. It should never return null, it's already strings, and the return type is IEnumerable.
public IEnumerable<string> GetDept()
{
    using (DeptEntities context = new DeptEntities())
    {
        return (from Department in db.Department orderby Department.ID
                select Department.Name
               ).Distinct();
    }
}

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The task of choosing a web design company to build a website for your business should never be taken in a light manner. Provided the fact that your website will act as a representative to your business and will be responsible for imposing an online …
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question