我正在动态构建一个以下开头的查询:
- var items = db.Items;
…
- case "4":
- items = items.OrderBy(x => x.Ratings.Average(t => t.score)).ThenBy(x => x.title);
问题是有些项目在评级表中还没有任何数据,所以我认为它试图对不存在的数据进行平均.最后使用DefaultOrEmpty()似乎没有任何效果.关于我如何解决这个问题的任何建议?
解决方法
- items = items.Where(x=>x.Ratings.Any()).
- OrderBy(x => x.Ratings.Average(t => t.score)).
- ThenBy(x => x.title);
试试吧.