SolvedPrivate

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

Posted on 2014-09-17
4
44 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

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 …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

734 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