Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9159427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:29:23+00:00 2026-06-17T13:29:23+00:00

I am fairly new to SQL Server, .NET and MVC 3/4, so please bear

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:29:25+00:00Added an answer on June 17, 2026 at 1:29 pm
    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am fairly new to SQL Server and trying to do something that seems
I'm fairly new to SQL, but trying to write quite a complicated query, but
We are building a new application in .net 3.5 with SQL server database. The
I'm fairly new to SQL Server and would really appreciate some help with this.
I'm fairly new to SQL Server and am still learning some of the tricks
I am fairly new to .net MVC 4 and WEB API. I need to
I am fairly new to SSAS and SQL Server but Google didn't help. I
Oracle's PL/SQL is fairly new to me, so I need some help understanding if
Alright, so I'm fairly new to PHP and SQL/MySQL so any help is appreciated.
I fairly new to JQuery and perhaps trying to achieve something that might be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.