I am using MongoDB for my database and I am using ASP.NET Core for my api. I am trying to convert my Items object to a ItemsDTO (Data Transfer Object), because I don't want to return the Id. However, when I use
_items.AsQueryable()
.Where(item => item.Game.Contains("Games/1"))
.Select(item => ItemToDTO(item))
.ToList();
It gives back a System.NotSupportedException: 'ItemToDTO of type Project.Services.ItemService is not supported in the expression tree ItemToDTO({document}).' By the way ItemToDTO looks like this:
private static ItemDTO ItemToDTO(Item item)
{
return new ItemDTO
{
Name = item.Name,
Type = item.Type,
Price = item.Price,
Game = item.Game,
CostToSell = item.CostToSell,
Description = item.Description
};
}
So I am confused on why this doesn't work because before I was using a SQL Server Database and normal Linq that looked like this:
_context.Items
.Where(item => item.Game.Contains("Games/1"))
.Select(item => ItemToDTO(item))
.ToList(),
and it worked the data that I wanted. I know that when using the MongoDB.Driver I am using their Linq and Queryable objects. Just wondering why I am getting the error above.