将linq转换为模型类失败“无法转换类型'System.Data.Entity.Infrastructure.DbQuery`1的对象”

努鲁·萨利胡(Nuru Salihu)

我正在尝试从下面的控制器将模型返回到我的视图。

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的对象”。

杰尼什·拉巴迪亚(Jenish Rabadiya)

只需执行以下简单查询即可。

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

我看不到需要创建Jobpost的新实例并设置所有属性。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档