将 linq 强制转换为模型类失败"Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery`1"



我正试图从下面的控制器将一个模型返回到我的视图中。

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);

我的视图收到一个JobPost类型的模型。下面是JobPost类

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; }
}

我该如何正确投射?就像我说的select new一样,这不是把类型改为匿名而不是DbQuery吗?错误读取

"无法强制转换类型为"System.Data.Entity.Infrastructure.DbQuery`1"的对象。

只需像下面这样的简单查询就可以了。

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

我认为没有必要创建Jobpost的新实例并设置所有属性。

JobPost model = (JobPost)在此处删除强制转换并使用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();

你也可以这样选择:

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();

我认为抛出此异常是因为您忘记在表达式的末尾添加FirstOrDefault

                  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);

最新更新