SolvedPrivate

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

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

685 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