Nuru Salihu
Nuru Salihu

Reputation: 4948

Casting linq to model class fails "Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery`1"

I am trying to return a model to my view from my controller below.

JobPost model = (JobPost)
                      (from posts in repository.JobPosts
                       orderby posts.PostDate descending
                       select new 
                       {
                           Id = posts.Id,
                           Post = posts.Post,
                           Logo = posts.Logo,
                           PostDate = posts.PostDate,
                           Employer = posts.Employer,
                           Region = posts.Region,
                           JobType = posts.JobType,
                           PostTitle = posts.PostTitle,
                           Industry = posts.Industry,
                           JobFunction = posts.JobFunction,
                           JobLevel = posts.JobLevel,
                           Salary = posts.Salary,
                           Experience = posts.Experience
                       }).Select(x => new JobPost
                       {
                           Id = x.Id,
                           Post = x.Post,
                           Logo = x.Logo,
                           PostDate = x.PostDate,
                           Employer = x.Employer,
                           Region = x.Region,
                           JobType = x.JobType,
                           PostTitle = x.PostTitle,
                           Industry = x.Industry,
                           JobFunction = x.JobFunction,
                           JobLevel = x.JobLevel,
                           Salary = x.Salary,
                           Experience = x.Experience
                       });

return View(model);

My view receives a model of type JobPost.Below is the jobPost Class

public class JobPost
{
    public long Id { get; set; }
    public string Post { get; set; }
    public string Logo { get; set; }
    public DateTime PostDate { get; set; }
    public string Employer { get; set; }
    public string Region { get; set; }
    public string JobType { get; set; }
    public string PostTitle { get; set; }
    public string Industry { get; set; }
    public string JobFunction { get; set; }
    public string JobLevel { get; set; }
    public decimal Salary { get; set; }
    public int Experience { get; set; }
}

How do I cast this the right way? As when I said select new, doesn't that change the type to anonymous instead of the DbQuery ? The error reads

"Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery`1".

Upvotes: 2

Views: 3739

Answers (3)

Alexander Efremov
Alexander Efremov

Reputation: 161

I think that this exception has been thrown because you forgot to add FirstOrDefault at the end of the expression

                  JobPost model = (JobPost)
                  (from posts in repository.JobPosts
                   orderby posts.PostDate descending
                   select new 
                   {
                       Id = posts.Id,
                       Post = posts.Post,
                       Logo = posts.Logo,
                       PostDate = posts.PostDate,
                       Employer = posts.Employer,
                       Region = posts.Region,
                       JobType = posts.JobType,
                       PostTitle = posts.PostTitle,
                       Industry = posts.Industry,
                       JobFunction = posts.JobFunction,
                       JobLevel = posts.JobLevel,
                       Salary = posts.Salary,
                       Experience = posts.Experience
                   }).Select(x => new JobPost
                   {
                       Id = x.Id,
                       Post = x.Post,
                       Logo = x.Logo,
                       PostDate = x.PostDate,
                       Employer = x.Employer,
                       Region = x.Region,
                       JobType = x.JobType,
                       PostTitle = x.PostTitle,
                       Industry = x.Industry,
                       JobFunction = x.JobFunction,
                       JobLevel = x.JobLevel,
                       Salary = x.Salary,
                       Experience = x.Experience
                   }).FirstOrDefault();

        return View(model);

Upvotes: 0

Jenish Rabadiya
Jenish Rabadiya

Reputation: 6766

Just a simple query like below should work.

JobPost model = (from post in repository.JobPosts
                   orderby post.PostDate descending
                   select post).FirstOrDefault();

I don't see any need to create new instance of Jobpost and setting all the properties.

Upvotes: 4

Giorgi Nakeuri
Giorgi Nakeuri

Reputation: 35790

JobPost model = (JobPost) remove casting here and use FirstOrDefault().

JobPost model =
                      (from posts in repository.JobPosts
                       orderby posts.PostDate descending
                       select new JobPost
                       {
                           Id = posts.Id,
                           Post = posts.Post,
                           Logo = posts.Logo,
                           PostDate = posts.PostDate,
                           Employer = posts.Employer,
                           Region = posts.Region,
                           JobType = posts.JobType,
                           PostTitle = posts.PostTitle,
                           Industry = posts.Industry,
                           JobFunction = posts.JobFunction,
                           JobLevel = posts.JobLevel,
                           Salary = posts.Salary,
                           Experience = posts.Experience
                       }).Select(x => new JobPost
                       {
                           Id = x.Id,
                           Post = x.Post,
                           Logo = x.Logo,
                           PostDate = x.PostDate,
                           Employer = x.Employer,
                           Region = x.Region,
                           JobType = x.JobType,
                           PostTitle = x.PostTitle,
                           Industry = x.Industry,
                           JobFunction = x.JobFunction,
                           JobLevel = x.JobLevel,
                           Salary = x.Salary,
                           Experience = x.Experience
                       }).FirstOrDefault();

Also you can select like this:

JobPost model =
                      (from posts in repository.JobPosts
                       orderby posts.PostDate descending
                       select new JobPost
                       {
                           Id = posts.Id,
                           Post = posts.Post,
                           Logo = posts.Logo,
                           PostDate = posts.PostDate,
                           Employer = posts.Employer,
                           Region = posts.Region,
                           JobType = posts.JobType,
                           PostTitle = posts.PostTitle,
                           Industry = posts.Industry,
                           JobFunction = posts.JobFunction,
                           JobLevel = posts.JobLevel,
                           Salary = posts.Salary,
                           Experience = posts.Experience
                       }).FirstOrDefault();

Upvotes: 1

Related Questions