Question

I am fairly new to SQL Server, .NET and MVC 3/4, so please bear with me. I had created a query in Linq-to-SQL that returned the data I needed, but unfortunately my host provider does not support the DbGeography class, specifically the DbGeography.PointFromText method Description here.

So I created a stored procedure, and I got the first part working, but I am having a hard time summarizing and averaging the data. I would like to return all data in the SELECT statement grouped by Dish.DishId, Rest.RerstaurantId, with the rev.Rating converted to AVG(rev.Rating) and a count column added for each group.

Here is the code that is working but not supported:

public static DbGeography CreatePoint(double latitude, double longitude)
{
     var text = string.Format(CultureInfo.InvariantCulture.NumberFormat,
                                     "POINT({0} {1})", longitude, latitude);
     return DbGeography.PointFromText(text, 4326);
 }
 public static double MilesToMeters(double? miles)
 {
     if (miles == null)
         return 0;
     return miles.Value * 1609.344;
}

[HttpGet]
public JsonResult TopRated(double lat, double lng, double miles)
{
     var source = CreatePoint(lat,lng);
     var dist = MilesToMeters(miles);
     var reviews = (from r in db.Reviews
                     where r.Restaurant.Location.Distance(source) <= dist
                     group r by new { 
                                      r.Dish,
                                      r.Restaurant.Name,
                                      r.Restaurant.Lat,
                                      r.Restaurant.Lng
                                     } into rg
                     select new { 
                                  Rating = rg.Average(r => r.Rating),
                                  Count = rg.Count(),
                                  Dish = rg.Key.Dish,
                                  Restaurant = new { 
                                                     Name = rg.Key.Name,
                                                     Lat = rg.Key.Lat,
                                                     Lng = rg.Key.Lng 
                                                   } 
                                 });                             

     reviews = reviews.OrderByDescending(r => r.Rating);
     return Json(reviews, JsonRequestBehavior.AllowGet);
 }

Here is the stored procedure that needs to be modified

CREATE PROCEDURE [dbo].[SearchNearbyReview] 
@Lat decimal(9,6),
@Lng decimal(9,6),
@Dist decimal(9,6)
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @pos geography;
    SET @pos = geography::STPointFromText('POINT(' 
                                          + CONVERT(varchar(15), @Lng) + ' ' 
                                          + CONVERT(varchar(15), @Lat) + ')', 
                                          4326)

    SELECT 
        rev.Rating, 
        rest.RestaurantId, 
        rest.Name AS RestaurantName, 
        rest.[Address], 
        rest.City, 
        rest.[State], 
        rest.Zip, 
        rest.Lat, 
        rest.Lng, 
        dish.DishId, 
        dish.Name AS DishName
    FROM  dbo.Reviews rev 
    INNER JOIN dbo.Restaurants rest 
        ON rev.Restaurant_RestaurantId = rest.RestaurantId
    INNER JOIN dbo.Dishes dish 
        ON rev.Dish_DishId = dish.DishId
    WHERE rest.Location.STDistance(@pos) <= (@Dist * 1609.334)
    ORDER BY rest.RestaurantId, dish.DishId
END
Was it helpful?

Solution

select 
    t.AvgRating,
    t.Count,
    t.DishName,
    t.DishId,
    rest.RestaurantId,
    rest.Name AS RestaurantName,
    rest.[Address],
    rest.City,
    rest.[State],
    rest.Zip,
    rest.Lat,
    rest.Lng
from dbo.Restaurants rest 
join (select 
        avg(rev.Rating) as AvgRating,
        count(1) as Count,
        Restaurant_RestaurantId,
        min(dish.Name) AS DishName,
        DishId
      from dbo.Dishes dish 
      join dbo.Reviews rev on rev.Dish_DishId = dish.DishId 
      group by Restaurant_RestaurantId, DishId
    ) as t
on t.Restaurant_RestaurantId = rest.RestaurantId
WHERE rest.Location.STDistance(@pos) <= (@Dist * 1609.334)
ORDER BY rest.RestaurantId, t.DishId
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top