0NLY777
0NLY777

Reputation: 313

Linq to SQL - Returning a count from two tables

Let me just start off by saying I am very much a beginner and being self taught a lot of the stuff I do is through trial and error, so please forgive me if I seem vague or if I don't explain myself clearly.

I'm currently building an auction website with MVC. I have two tables, one for the auctions and one for bids. There is a foreign key relationship between the column AuctionID on both tables. I need to return a list of the top 5 auctions based on the amount of bids each item has received. At the moment I just want to list the auction title and the amount of bids it has received, something like this > 'A Lovely Item - 25 bids' etc etc.

I have had a go at a few things but to be honest I'm not really sure of the correct way to go about this. I'm using Linq to SQL and MVC. Any help is greatly appreciated, thanks.

Upvotes: 0

Views: 543

Answers (2)

Rami Alshareef
Rami Alshareef

Reputation: 7140

Since you are a self-taught style, check the sample application provided by Microsoft for LINQ for more info

Located here: C:\Program Files\Microsoft Visual Studio 9.0\Samples\1033\CSharpSamples.zip

change drive C with VS 2008 installed drive

Upvotes: 0

Justin Niessner
Justin Niessner

Reputation: 245419

If you defined the keys properly and have Bids set up as a Navigation Property of Auctions, then you should be able to use a simple combination of OrderyBy and Take:

var topFiveByBids = db.Auctions.OrderBy(a => a.Bids.Count()).Take(5);

Upvotes: 2

Related Questions