To my Book entity I want to query to select required properties and return this as a list to the another List type of<BookModel>
from which I'll be using as a model in my view. But in this line of code red squiggly line indicates error with the message :
cannot convert system.collections.generic.List<anonymous> to system.collections.generic.List<BookModel>
Of course I can use Select(b=> new BookModel{...})
class instead of anonymous type to creat list object type but I'm wondering if there's a way to return this without using BookModel, only by using anonymous type?
here is the property of List type that I want to return my anonymous type
public class HomeModel
{
public List<BookModel> PopularBooks { get; set; }
public List<BookModel> BestSales { get; set; }
}
and the action method that I'm making queries using lambda expressions.
public ActionResult HomeIndex()
{
HomeModel model = new HomeModel();
var bestSalebooks = db.Books.Where(b => b.IsApproved).OrderBy(b => b.DisplayNumber).Select(b => new
{
Id = b.Id,
Name = b.Name,
Description = b.Description,
Price = b.Price,
DateAdded = b.DateAdded,
CategoryId = b.CategoryId
}).ToList();
model.BestSales = bestSalebooks; //cannot convert system.collections.generic.List<anonymous> to system.collections.generic.Lis<BookModel>
return View(model);
}