0

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);
}
Cavid
  • 125
  • 13
  • 10
    Perhaps instead of `new { ... }` you just need `new BookModel { ... }`? Do you *need* to use an anonymous type? – Jon Skeet May 20 '19 at 10:54
  • @JonSkeet this is just out of curiosity.. not have any requirement. – Cavid May 20 '19 at 10:57
  • Possible duplicate of [How to cast List to List](https://stackoverflow.com/questions/8933434/how-to-cast-listobject-to-listsomethingelse) – Heretic Monkey May 20 '19 at 10:58
  • 1
    Anonymous types are convenient if some processing requires us to create something temporary and then extract something else from it. In that scenario the members of the anonymous type might be really obvious, but maybe it's a one-off use and it would seem excessive to create a class for something that's used momentarily within a method. If you know that your data represents a `BookModel` then it almost certainly makes more sense to skip the anonymous type and just use the class. – Scott Hannen May 20 '19 at 13:26

1 Answers1

3

In general, I would advise you to instantiate your required type. That is, if you need a BookModel, instantiate that instead of creating an anonymous type.

You are creating an anonymous type when you do the Select step. Anyway you can also cast the anonymous type with your specific type by doing:

public ActionResult HomeIndex()
{
    HomeModel model = new HomeModel();
    var bestSalebooks = db.Books.Where(b => b.IsApproved).OrderBy(b => b.DisplayNumber).Select(b => (BookModel)(new
    {
        Id =  b.Id,
        Name = b.Name,
        Description = b.Description,
        Price = b.Price,
        DateAdded = b.DateAdded,
        CategoryId = b.CategoryId

    })).ToList();
    model.BestSales = bestSalebooks;            
    return View(model);
}

Doing this, you are creating a list of BookModel.

Scott Hannen
  • 27,588
  • 3
  • 45
  • 62
Giovanni Patruno
  • 651
  • 9
  • 15