0

Model class:

public class MonthlySalesOrders : BaseNopEntityModel
{

    public ICollection<Nop.Core.Domain.Orders.OrderItem> Categories { get; set; }
}

Controller Action

    private PagedList.IPagedList<MonthlySalesOrders> orderByDateAdded()
    {

        var ord = _orderService.SearchOrders();
        var daylyOrderReport = (from o in ord.AsEnumerable()
                                group o by new { Quarter = ((o.CreatedOnUtc.CompareTo(DateTime.UtcNow.AddDays(-7)))) } into orGrp
                                select new MonthlySalesOrders
                                {  Categories = (ICollection<Nop.Core.Domain.Orders.OrderItem>)orGrp.Select(c => c.OrderItems)
                                })

        return daylyOrderReport.ToPagedList(1, 10);
    }

From above "controller action" I have used linq query to select orderlist from "orGrp group", and into "Model class" I have declared a public ICollection<Nop.Core.Domain.Orders.OrderItem> Categories { get; set; }, so when I build and ran the above code it displays following error messages.

Unable to cast object of type 'WhereSelectEnumerableIterator2[Nop.Core.Domain.Orders.Order,System.Collections.Generic.ICollection1[Nop.Core.Domain.Orders.OrderItem]]' to type 'System.Collections.Generic.ICollection`1[Nop.Core.Domain.Orders.OrderItem]'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Unable to cast object of type 'WhereSelectEnumerableIterator2[Nop.Core.Domain.Orders.Order,System.Collections.Generic.ICollection1[Nop.Core.Domain.Orders.OrderItem]]' to type 'System.Collections.Generic.ICollection`1[Nop.Core.Domain.Orders.OrderItem]'.

Source Error:

Line 2374: var daylyOrderReport = (from o in ord.AsEnumerable() Line 2375: group o by new { Quarter = ((o.CreatedOnUtc.CompareTo(DateTime.UtcNow.AddDays(-7)))) } into orGrp Line 2376: select new MonthlySalesOrders Line 2377: { Line 2378: Year = orGrp.First().CreatedOnUtc.Year.ToString(),

and I want get category list from three level nested reference from orGrp like Categories =(ICollection<Nop.Core.Domain.Orders.OrderItem>) orGrp.Select(gb => gb.OrderItems.Select(s => s.Product.ProductManufacturers)) so please help me how can I do this.

har07
  • 88,338
  • 12
  • 84
  • 137

1 Answers1

1

You can use the ToList function which returns an IList which implements ICollection. Also, it looks like c.OrderItems is an enumerable of items, not a single item. If this is the case then you'll want to flatten list returned with SelectMany.

orGrp.SelectMany(c => c.OrderItems).ToList()
Community
  • 1
  • 1
Steven Wexler
  • 16,589
  • 8
  • 53
  • 80