无法从“ System.Collections.Generic.List <对象>”转换为“ System.Collections.Generic.List <dal.model>”

时间:2018-08-29 11:04:39

标签: asp.net-web-api entity-framework-6

我想从模型表中检索所有确定的列作为可查询的 我写的代码显示我

  

严重性代码描述项目文件行抑制状态   错误CS1503参数1:无法从“ System.Collections.Generic.List”转换为“ System.Collections.Generic.List”

通过最佳实践来解决此错误的任何帮助:**

public IQueryable<DAL.model> GetAllmodels()
    {
        var models = (from d in db.models
                      where (d.Model_Deleted == false)
                      select (
                      new
                      {
                          d.Model_ID,
                          d.Model_Name,
                          d.Model_Image

                      })).AsQueryable();

        return models;
    }

1 个答案:

答案 0 :(得分:1)

而不是使用anonymous选择new { .. }对象,请尝试使用new DAL.model() { ... }进行以下操作。

public IQueryable<DAL.model> GetAllmodels()
{
    var models = (from d in db.models
                  where (d.Model_Deleted == false)
                  select (
                  new DAL.model()
                  {
                      Model_ID = d.Model_ID,
                      Model_Name = d.Model_Name,
                      Model_Image = d.Model_Image
                  })).AsQueryable();

    return models;
}