Susan
Susan

Reputation: 1832

Linq to Entities: complex query getting "average" restaurant rating

So I'm building a Restaurant Review site for my community. I need to extract data from the following tables: RESTAURANT, CUISINE, CITY, PRICE and RATING (customer ratings).

The query should return all restuarants of a selected CUISINE_ID and return the RESTAURANT_NAME, CUSINE_NAME, CUTY_NAME, PRICE_CODE and it should average all the reviews RATING_CODE and return a calculated value. I'm fine with returning all the data except the average rating.

I've only been working with LINQ to Entities 2 days and LINQ for about 3 weeks, so I'm really a newbie; I'm waiting for my LINQ book to be delivered from Amazon.com. Your help guidance be appreciated!

Upvotes: 0

Views: 2851

Answers (2)

StriplingWarrior
StriplingWarrior

Reputation: 156524

It should end up looking something like this:

var avgForMatches = 
    (from r in context.Restaurants
     where r.Cuisines.Any(c => c.CuisineName == cuisineName)
     where r.Prices.Any(p => p.PriceCode == priceCode)
     //... same pattern for other searches.
     select r.RatingCode)
    .Average();

Upvotes: 1

Stuart
Stuart

Reputation: 66882

Read about aggregate methods (including average) within the 101 linq samples - http://msdn.microsoft.com/en-us/vcsharp/aa336747

Upvotes: 0

Related Questions