繁体   English   中英

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

[英]Casting linq to model class fails “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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM