-2

The PriceOffer property is nullable of decimal

 List<TopRated> TopRated = (from review in db.ProductReviews
                            group review by review.ProductID into product
                            select new TopRated
                            {
                              ProductName = product.Select(p => p.Products.Name).FirstOrDefault(),
                              Price = product.Select(p => p.Products.Price).FirstOrDefault(),
                              PriceOffer = product.Select(p => p.Products.PriceOffer).FirstOrDefault(),
                              ProductId = product.Key,
                              AverageRating = product.Average(p => p.Rating)
                            }).ToList();
OrElse
  • 9,709
  • 39
  • 140
  • 253

2 Answers2

12

To convert from decimal? to decimal, try Value:

decimal? d = 0;
decimal d1 = d.Value;

As @Tim commented, it throws exception if decimal? has no value.

Depending on your logic, you may try something like

decimal d1 = d.HasValue? d.Value: 0;

or just

decimal d2 = d ?? 0;
AlexD
  • 32,156
  • 3
  • 71
  • 65
0

Probably this product.Select(p => p.Products.PriceOffer).FirstOrDefault() returns a decimal type. You can't type decimal to nullable decimal.

Here you can fin examples of such a casting: How can I convert decimal? to decimal

Community
  • 1
  • 1
Andrius
  • 136
  • 4
  • "You can't type decimal to nullable decimal." - it is the other way around - as the error message indicates. – Igor Oct 09 '15 at 18:32