2013-10-24 81 views
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 
       }); 

但它也需要按平均分數排序。

+2

分發布提問之前請出示努力。 Stack Overflow上很少有人想爲你做你的工作。你嘗試過什麼嗎?什麼是工作,什麼不工作? –

+0

你好,我aleary有一個查詢組由resto,但我想奧伯由平均三個評級的平均值... var restosss =(從w db.restos_cote組w通過w.idRestoSuccursale到g選擇新{idRestoSuccursale = g.Key,Restos = g}); – user2913658

回答

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