SolvedPrivate

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

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
To properly understand GitHub, let’s divide it into two words ‘Git’ and ‘Hub’. Git is basically a ‘Distribution Version Control’ (DVC) and ‘Source Code Management’ (SCM) system widely used by software programmers while Hub means the efficient centre…
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 demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now