Hi svibuk,
You need to group by ProductName like below and create model class with two properties which hold the value of the ProductName and Count because by using ViewBag and looping the ViewBag in View will throw the error and you cant simply convert the values so loop through the values stored in list and add them in List of Model Class and return it to the view and loop through the list of model class in View and access the value and i have created a sample for better understanding refer below.
var details = db.Transaction.GroupBy(n => n.ProductName //Column Name which holds the value of ProductName).
Select(group =>
new
{
ProductName = group.Key,
Count = group.Count()
}).ToList();
I have created a sample which full fill your requirement you need to modify the code according to your need.
SQL
CREATE TABLE [Images](
[Id] [int] NOT NULL,
[Name] [varchar](30) NOT NULL,
[Url] [nvarchar](200) NOT NULL,
[CategoryId] [int] NULL,
[CategoryName] [varchar](50) NULL
) ON [PRIMARY]
INSERT [Images] ([Id], [Name], [Url], [CategoryId], [CategoryName]) VALUES (1, N'Fall 714', N'http://farm3.staticflickr.com/2854/10380193164_9b65e4c5ed_n.jpg', 1, N'First Category')
INSERT [Images] ([Id], [Name], [Url], [CategoryId], [CategoryName]) VALUES (2, N'Fall 707', N'http://farm8.staticflickr.com/7395/10380186284_4f9ac522ed_n.jpg', 1, N'First Category')
INSERT [Images] ([Id], [Name], [Url], [CategoryId], [CategoryName]) VALUES (3, N'Fall 716', N'http://farm8.staticflickr.com/7424/10380408813_3cd984570d_n.jpg', 1, N'First Category')
INSERT [Images] ([Id], [Name], [Url], [CategoryId], [CategoryName]) VALUES (4, N'Blue Columbine', N'http://farm4.staticflickr.com/3723/8986453414_a869ddc3aa_n.jpg', 1, N'First Category')
INSERT [Images] ([Id], [Name], [Url], [CategoryId], [CategoryName]) VALUES (5, N'1 January 2012 007', N'http://farm8.staticflickr.com/7130/6885958326_ecbb33e962_n.jpg', 2, N'Second Category')
INSERT [Images] ([Id], [Name], [Url], [CategoryId], [CategoryName]) VALUES (6, N'1 January 2012 001', N'http://farm8.staticflickr.com/7051/7032054587_5f15e32a10_n.jpg', 2, N'Second Category')
INSERT [Images] ([Id], [Name], [Url], [CategoryId], [CategoryName]) VALUES (7, N'1 January 2012 013', N'http://farm8.staticflickr.com/7228/6885959482_670fb32b58_n.jpg', 2, N'Second Category')
INSERT [Images] ([Id], [Name], [Url], [CategoryId], [CategoryName]) VALUES (8, N'1 January 2012 017', N'http://farm8.staticflickr.com/7120/7032057469_ce0c1620d4_n.jpg', 2, N'Second Category')
INSERT [Images] ([Id], [Name], [Url], [CategoryId], [CategoryName]) VALUES (9, N'Fall 065', N'http://farm3.staticflickr.com/2828/10379529486_7abd3b99ef_n.jpg', 3, N'Third Category')
INSERT [Images] ([Id], [Name], [Url], [CategoryId], [CategoryName]) VALUES (10, N'Fall 053', N'http://farm4.staticflickr.com/3723/10379498815_2acedf4c6c_n.jpg', 3, N'Third Category')
INSERT [Images] ([Id], [Name], [Url], [CategoryId], [CategoryName]) VALUES (11, N'Fall 595', N'http://farm8.staticflickr.com/7397/10379841244_ec8c68ccc4_n.jpg', 3, N'Third Category')
INSERT [Images] ([Id], [Name], [Url], [CategoryId], [CategoryName]) VALUES (12, N'Fall 227', N'http://farm6.staticflickr.com/5528/10379655586_2a95e5f127_n.jpg', 3, N'Third Category')
INSERT [Images] ([Id], [Name], [Url], [CategoryId], [CategoryName]) VALUES (13, N'Fall 192', N'http://farm3.staticflickr.com/2837/10379605235_5e95702e08_n.jpg', 4, N'Fourth Category')
INSERT [Images] ([Id], [Name], [Url], [CategoryId], [CategoryName]) VALUES (14, N'Fall 249', N'http://farm3.staticflickr.com/2820/10379652095_983088cf4c_n.jpg', 4, N'Fourth Category')
INSERT [Images] ([Id], [Name], [Url], [CategoryId], [CategoryName]) VALUES (15, N'Fall 254', N'http://farm8.staticflickr.com/7380/10379642624_89b111ef11_n.jpg', 4, N'Fourth Category')
INSERT [Images] ([Id], [Name], [Url], [CategoryId], [CategoryName]) VALUES (16, N'Fall 525', N'http://farm8.staticflickr.com/7460/10379808224_336a427151_n.jpg', 4, N'Fourth Category')
Model => DisplayImage.cs
public class DisplayImage
{
public string Categoryname { get; set; }
public int Count { get; set; }
}
Controller => HomeController
public ActionResult Index()
{
//Your EntityModel or bussiness logic for fetching the records.
TestEntities entities = new TestEntities();
var images = entities.Images.GroupBy(n => n.CategoryName)
.Select(g => new { CategoryName = g.Key, Count = g.Count() }).ToList();
List<DisplayImage> imagesList = new List<DisplayImage>();
for (int i = 0; i < images.ToList().Count; i++)
{
imagesList.Add(new DisplayImage { Categoryname = images[i].CategoryName, Count = images[i].Count });
}
return View(imagesList);
}
View => Index.cshtml
@model IEnumerable<GroupByEntityFrameWork.DisplayImage>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<table>
<tr>
<th>Customer Name</th>
<th>Count</th>
</tr>
@foreach (DisplayImage item in Model)
{
<tr>
<td>@item.Categoryname</td>
<td>@item.Count</td>
</tr>
}
</table>
</div>
</body>
</html>
OutPut
CategoryName |
Count |
First Category |
4 |
Fourth Category |
4 |
Second Category |
4 |
Third Category |
4 |