Need a help to get count by using query on mvc

How to a count of some product by grouping them and the names of products too using MVC?

The following query I made, but after that I don't know what to do.

var qrystr = new StringBuilder();
qrystr.Append("select SelectedPersonId, count(PersonName) as CountData from [dbo].[EntryMaster] Group by SelectedPersonId");

I need to show the SelectedPersonId, and the CountData... How can I solve this through MVC?

Upvotes: 0

Views: 154

Answers (2)

adiga
adiga

Reputation: 35212

If you're using entity framework, you can use linq to query your data set:

context.EntryMaster
     .GroupBy(e => e.SelectedPersonId)
     .Select(g => new { SelectedPersonId = g.Key, CountData = g.Count() })
     .ToList()

This returns a List of objects with 2 properties: SelectedPersonId and CountData

Upvotes: 2

frank Walleway
frank Walleway

Reputation: 136

Cant you just add .Count() at the end of the qrystr.Append()?

Upvotes: 0

Related Questions