I got LINQ to Entities does not recognize the method 'System.String GetName(System.Type, System.Object)' method, and this method cannot be translated into a store expression.
How can i fix that? I did not find a suitable example for my case.
return context.Books.Select(e => new BookViewModel
{
BookId = e.BookId,
BookName = e.BookName,
Genre = new GenreViewModel
{ GenreName = Enum.GetName(typeof(Genre), (int)e.Genre), GenreId = (int)e.Genre },// error is here
Pages = e.Pages,
Publisher = e.Publisher,
Authors = e.Authors.Select(t => new AuthorViewModel
{
AuthorId = t.AuthorId,
AuthorName = t.AuthorName
}).ToList()
});
Models:
public enum Genre
{
[Description("Comedy")]
Comedy,
[Description("Drama")]
Drama,
[Description("Horror")]
Horror,
[Description("Realism")]
Realism,
[Description("Romance")]
Romance,
[Description("Satire")]
Satire,
[Description("Tragedy")]
Tragedy,
[Description("Tragicomedy")]
Tragicomedy,
[Description("Fantasy")]
Fantasy,
[Description("Mythology")]
Mythology,
[Description("Adventure")]
Adventure,
}
public class GenreViewModel
{
public int GenreId { get; set; }
public string GenreName { get; set; }
}