I'm dynamically building up a query starting with this:
var items = db.Items;
...
case "4":
items = items.OrderBy(x => x.Ratings.Average(t => t.score)).ThenBy(x => x.title);
The problem is that some items don't have any data in the ratings table yet, so I believe it's trying to average over data that doesn't exist. Using DefaultOrEmpty()
at the end doesn't seem to have any effect. Any suggestions on how I would fix this?
If you are using this form:
var effectiveFloor =
policies
.Where(p => p.PricingStrategy == PricingStrategy.EstablishFloor)
.Max(p => p.Amount);
Then the solution is:
var effectiveFloor =
policies
.Where(p => p.PricingStrategy == PricingStrategy.EstablishFloor)
.DefaultIfEmpty()
.Max(p => p==null ? 0 : p.Amount);
Found here
items = items.Where(x=>x.Ratings.Any()).
OrderBy(x => x.Ratings.Average(t => t.score)).
ThenBy(x => x.title);
Try that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With