2015-05-02 37 views
1

我有下面的代碼,出現以下錯誤:ASP.NET轉換的IQueryable列出

Error 1 Cannot implicitly convert type 'System.Linq.IQueryable<Receptura_v4.Models.ReceptDTO>' to 'Receptura_v4.Models.ReceptDTO' . An explicit conversion exists (are you missing a cast?)

任何想法如何解決呢?

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

回答

0

該錯誤表示您的返回類型與您返回的內容不匹配。你的返回類型爲ReceptDTO,你正在返回一個類型的IQueryable<ReceptDTO>

您可以通過從IQueryable

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對象如果需要返回一個列表,你可以更改方法的返回類型和返回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(); 
    } 
} 
0

此代碼列表:

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

Asssigns qIQueryable<ReceptDTO>

雖然你的原型只需要一個ReceptDTO的返回類型:

public ReceptDTO GetReceptForUser(string userId) 


如果你想返回IQueryable<ReceptDTO>然後改變你的原型:

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