c# - 将 linq 转换为模型类失败 "Unable to cast object of type ' System.Data.Entity.Infrastruct.DbQuery`1"

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

我正在尝试从下面的 Controller 将模型返回到我的 View 。

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

我的 View 接收 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 吗?错误内容为

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

最佳答案

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

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

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

关于c# - 将 linq 转换为模型类失败 "Unable to cast object of type ' System.Data.Entity.Infrastruct.DbQuery`1",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28625190/

相关文章:

.net - 无法将 LINQ to SQL 类添加到 VS2010 中的项目

c# - MVC 在同一用户登录后注销所有事件 session

c# - 2个Linq查询可以并行运行吗?

c# - SaveChanges 后获取所有对象的标识

c# - 从动态创建的事件处理程序访问控制属性

C# - 运算符 : ? - 是否可以向函数传递某些内容或不传递任何内容?

c# - 将 Include() 重写为 linq 连接

c# - Skip and Take 后对象字典的转换结果

asp.net - 使用 Html 代替 csHtml

javascript - LinqJs - 使用非重复计数进行分组