将linq转换为模型类失败"无法转换类型&System; System.Data.Entity.Infrastructure.DbQuery`1"

时间:2015-02-20 09:09:12

标签: c# asp.net-mvc linq entity-framework

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

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类型的模型.Below是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”类型的对象。

3 个答案:

答案 0 :(得分:3)

只需像下面这样的简单查询即可。

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

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我认为抛出此异常是因为您忘记在表达式末尾添加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);