【问题标题】:ASP.NET convert IQueryable to ListASP.NET 将 IQueryable 转换为 List
【发布时间】:2015-07-12 05:21:19
【问题描述】:

我收到以下代码,但出现以下错误:

错误 1 ​​无法隐式转换类型 'System.Linq.IQueryable<Receptura_v4.Models.ReceptDTO>''Receptura_v4.Models.ReceptDTO'。存在显式转换(是 你错过了一个演员?)

有什么办法解决吗?

public ReceptDTO GetReceptForUser(string userId)
{
    using (ApplicationDbContext db = new ApplicationDbContext())
    { 
        var q = from r in db.Recipes where r.UserId == userId select new ReceptDTO
            {
                 RecipeID = r.RecipeID,
                 Title = r.Title,
                 Portion = r.Portion,
                 Time = r.Time,
                 Category = r.Category,
                 Preparation = r.Preparation,
                 Difficulty = r.Difficulty,
                 Views = 0,
                 Price = r.Price,
                 Date = DateTime.Now,
                 UserId = userId
            };
        return q;
    }
}

【问题讨论】:

    标签: c# asp.net asp.net-mvc linq asp.net-mvc-5


    【解决方案1】:

    错误意味着您的返回类型和您返回的内容不匹配。你的返回类型是ReceptDTO,你返回的类型是IQueryable<ReceptDTO>

    您可以通过从IQueryable 发回第一个对象来返回ReceptDTO 对象

    public ReceptDTO GetReceptForUser(string userId)
    {
        using (ApplicationDbContext db = new ApplicationDbContext())
        { 
            var q = from r in db.Recipes where r.UserId == userId select new ReceptDTO
                {
                     RecipeID = r.RecipeID,
                     Title = r.Title,
                     Portion = r.Portion,
                     Time = r.Time,
                     Category = r.Category,
                     Preparation = r.Preparation,
                     Difficulty = r.Difficulty,
                     Views = 0,
                     Price = r.Price,
                     Date = DateTime.Now,
                     UserId = userId
                };
            return q.FirstOrDefault();
        }
    }
    

    如果你需要返回一个列表,你可以改变你的方法返回类型,返回一个ReceptDTO的列表

    public List<ReceptDTO> GetReceptForUser(string userId)
    {
        using (ApplicationDbContext db = new ApplicationDbContext())
        { 
            var q = from r in db.Recipes where r.UserId == userId select new ReceptDTO
                {
                     RecipeID = r.RecipeID,
                     Title = r.Title,
                     Portion = r.Portion,
                     Time = r.Time,
                     Category = r.Category,
                     Preparation = r.Preparation,
                     Difficulty = r.Difficulty,
                     Views = 0,
                     Price = r.Price,
                     Date = DateTime.Now,
                     UserId = userId
                };
            return q.ToList();
        }
    }
    

    【讨论】:

      【解决方案2】:

      这段代码:

      var q = from r in db.Recipes where r.UserId == userId select new ReceptDTO
                      {
                           RecipeID = r.RecipeID,
                           Title = r.Title,
                           Portion = r.Portion,
                           Time = r.Time,
                           Category = r.Category,
                           Preparation = r.Preparation,
                           Difficulty = r.Difficulty,
                           Views = 0,
                           Price = r.Price,
                           Date = DateTime.Now,
                           UserId = userId
                      };
      

      q 分配给IQueryable&lt;ReceptDTO&gt;

      虽然您的原型需要单个 ReceptDTO 的返回类型:

      public ReceptDTO GetReceptForUser(string userId)
      


      如果您想返回 IQueryable&lt;ReceptDTO&gt;,请将您的原型更改为:

      public IQueryable<ReceptDTO> GetReceptForUser(string userId)
          {
              using (ApplicationDbContext db = new ApplicationDbContext())
              { 
                  var q = from r in db.Recipes where r.UserId == userId select new ReceptDTO
                      {
                           RecipeID = r.RecipeID,
                           Title = r.Title,
                           Portion = r.Portion,
                           Time = r.Time,
                           Category = r.Category,
                           Preparation = r.Preparation,
                           Difficulty = r.Difficulty,
                           Views = 0,
                           Price = r.Price,
                           Date = DateTime.Now,
                           UserId = userId
                      };
                  return q;
              }
          }
      

      如果您想从查询中返回第一个项目,请添加 FirstOrDefault()

          public ReceptDTO GetReceptForUser(string userId)
          {
              using (ApplicationDbContext db = new ApplicationDbContext())
              { 
                  var q = from r in db.Recipes where r.UserId == userId select new ReceptDTO
                      {
                           RecipeID = r.RecipeID,
                           Title = r.Title,
                           Portion = r.Portion,
                           Time = r.Time,
                           Category = r.Category,
                           Preparation = r.Preparation,
                           Difficulty = r.Difficulty,
                           Views = 0,
                           Price = r.Price,
                           Date = DateTime.Now,
                           UserId = userId
                      };
                  return q.FirstOrDefault();
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2012-11-15
        • 2010-11-18
        • 1970-01-01
        • 2012-02-07
        • 2010-10-19
        • 2016-08-16
        • 2020-11-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多