SolvedPrivate

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

Posted on 2014-09-17
4
40 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 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