jimminybob
jimminybob

Reputation: 1442

Retrieving count of records from SQL table in MVC

I have a piece of sql similar to the code below that I wish to execute in my MVC site and display the results in the view:

select top 10 username, count(*)
from databasetable
group by username
order by count(*) desc

I've seen in many places that Entity Framework is the best way to do this, but i'm struggling to get it started and how to integrate it into my site. Can someone give me a push in the right direction on how to do it?

Thanks

Upvotes: 0

Views: 1350

Answers (1)

StackTrace
StackTrace

Reputation: 9416

If you want to run queries using raw SQL directly against the database then have a look at the code snippet below.

     using (var context = new YourContext()) 
         { 
             var blogs = context.YourTable.SqlQuery("SELECT * FROM dbo.YourTable").ToList(); 
         }

See this link http://msdn.microsoft.com/en-us/data/jj592907.aspx

Substitute the select query with your own query.

Upvotes: 1

Related Questions