0

My application is MVC 3.1. I'm not sure what could be the reason for "System.InvalidOperationException". Any help is appreciated!

public dynamic MostBoughtItems()
{
    return _orderRepository.MostBoughtItems();
}

//Repository _orderRepository:

public dynamic MostBoughtItems()

    {
        return _DbContext.Orders.GroupBy(x => x.ItemTitle, x => x.Quantity).Where(g => g.Count() > 1).OrderByDescending(g => g.Count()).Select(g => g.Key).Take(3);
    }
    

public List<MenuItem> GetTopTreeMenuItems()
    {
        List<MenuItem> topMenuItems = new List<MenuItem>();
        var threeFav = MostBoughtItems();
        
        foreach(var item in threeFav)
        {
            string title = Convert.ToString(item);
            var singleItem = _menuItemService.GetMenuItemByTitle(title);
            topMenuItems.Add(singleItem);
        }
        return topMenuItems;
    }

// Here is the line where the error shows:

public MenuItem GetMenuItemByTitle(string itemTitle)
{
    return _DbContext.MenuItems.FirstOrDefault(x => x.Title.Contains(itemTitle));
}
robertpit
  • 29
  • 4

0 Answers0