0

我想在我的数据库中获得最好的五家餐厅。数据库的结构使我的 linq 查询复杂化。

这就是我的工作方式,我有一张“评论”表,其中包含我餐厅的外键,我有 1 到 5 之间的三个选项可供选择。

表格示例:

- idRestaurant
- ratingOne
- ratingTwo
- ratingThree

因此,我会用三个音符的平均值对餐厅进行分类

例如,如果我在表中有一些记录

idRestaurant 1 - RatingOne 2 - RatingTwo 2 - RatingThree - 2

idRestaurant 1 - RatingOne : 4 - RatingTwo : 4 - RatingThree - 4

idRestaurant 2 - RatingOne : 3 - RatingTwo : 3 - RatingThree - 3

idRestaurant 2 - RatingOne : 4 - RatingTwo : 4 - RatingThree - 4

idRestaurant : 3 - RatingOne 1 - RatingTwo 1 - RatingThree - 1

idRestaurant : 3 - RatingOne 1 - RatingTwo 1 - RatingThree - 1

idRestaurant : 4 - RatingOne 1 - RatingTwo 1 - RatingThree - 1

idRestaurant : 4 - RatingOne 1 - RatingTwo 1 - RatingThree - 1

idRestaurant : 4 - RatingOne 1 - RatingTwo 1 - RatingThree - 1

idRestaurant : 4 - RatingOne 5 - RatingTwo 5 - RatingThree - 5

我希望我的 LINQ 的结果按总分的顺序向我发送 idRestaurants 列表,而没有重复的餐厅示例:{ 2,1,4,3 }

我有以下查询

var restosss = (from w in db.restos_cote 
                group w by w.idRestoSuccursale into g 
                select new 
                {
                   idRestoSuccursale = g.Key, Restos = g 
                });

但也需要按平均分排序。

4

1 回答 1

0

那么三个值的平均值只是val1 + val2 + val3 / 3

然后你可以使用Average这个计算。

var restosss = (from w in db.restos_cote 
                group w by w.idRestoSuccursale into g 
                select new 
                {
                   idRestoSuccursale = g.Key, Restos = g
                   avgRating = g.Average(x => (x.RatingOne + x.RatingTwo + x.RatingThree) / 3.0)
                })
                //if you want "best ratings" first, else use OrderBy
                .OrderByDescending(m => m.avgRating)
                //take only the 5 best ratings
                .Take(5);

顺便说一句,如果你只想要一个正确的排序,但不使用平均结果,你不需要除以 3.0

于 2013-10-24T06:33:51.190 回答