'results group by month based on the datetime c#
In my ASP.NET MVC application, there is a line chart in which I want to show record counts and the month on X-axis and Y-axis.
So I'm trying to call an ajax to the controller and the model has a record created date and the value.
I can't figure out how to return it by grouping it like the month of April has 15 records. The month of May has 12 records... likewise.
In my model structured like this,
public class CustomerFeedBack {
[Key]
public int Id {
get;
set;
}
public DateTime CreatedDate {
get;
set;
} = DateTime.Now;
public int TotalSatisfaction {
get;
set;
}
}
This is the javascript of the chart, here in the data I want to return the count of records and for labels months. Currently, it has dummy data.
var bouData = {
// Generate the days labels on the X axis.
labels: Array.from(new Array(30), function (_, i) {
return i === 0 ? 1 : i;
}),
datasets: [{
label: 'Satisfied',
fill: 'start',
data: [1500, 800, 320, 180, 240, 320, 230, 650, 590, 1200, 750, 940, 1420, 1200, 960, 1450, 1820, 2800, 2102, 1920, 3920, 3202, 3140, 2800, 3200, 3200, 3400, 2910, 3100, 4250],
backgroundColor: 'rgba(0,123,255,0.1)',
borderColor: 'rgba(0,123,255,1)',
pointBackgroundColor: '#ffffff',
pointHoverBackgroundColor: 'rgb(0,123,255)',
borderWidth: 1.5,
pointRadius: 0,
pointHoverRadius: 3
},
So in the controller, I want to return JsonResult and bind to labels as month and data as count.
This is my incomplete controller code.
var satisfied = (from a in db.tbl_Main where a.TotalSatisfaction >= 12
select new {
// Here I want to check with the create date group it as Month and sum the value of a.TotalSatisfaction count.
}).ToList();
I'm not an experienced developer regarding this, Still learning and would appreciate if you can suggest if there is another way of doing this easily or how to complete this.
Thanks.
Updating the question May 19, 2022
I have applied this code within the jasonresult
var satisfied = db.tbl_Main.Where(m => m.TotalSatisfaction >= 12).GroupBy(
m => new { m.CreatedDate.Year, m.CreatedDate.Month },
m => m.TotalSatisfaction
).ToList();
It's shows like this
It shows the count 4 which means for the Month of 05 it has 4 records.
I printed the results in the console and it shows as
Here I want to know, I want to show these months and the counts in the line chart. So I hope, If I could return the data like Month count I will be able to show it on the chart right? or any ideas from the experience?
This below script is I'm returning the values, so hoping to show in the labels month and the data section count.
var bouData = {
// Generate the days labels on the X axis.
labels: Array.from(new Array(30), function (_, i) {
return i === 0 ? 1 : i;
}),
datasets: [{
label: 'Satisfied',
fill: 'start',
data: [1500, 800, 320, 180, 240, 320, 230, 650, 590, 1200, 750, 940, 1420, 1200, 960, 1450, 1820, 2800, 2102, 1920, 3920, 3202, 3140, 2800, 3200, 3200, 3400, 2910, 3100, 4250],
backgroundColor: 'rgba(0,123,255,0.1)',
borderColor: 'rgba(0,123,255,1)',
pointBackgroundColor: '#ffffff',
pointHoverBackgroundColor: 'rgb(0,123,255)',
borderWidth: 1.5,
pointRadius: 0,
pointHoverRadius: 3
},
Solution 1:[1]
This should be translatable to SQL by whatever ORM you're using:
var satisfied = db.tbl_Main
.Where(m => m.TotalSatisfaction >= 12)
.GroupBy(
m => new { m.CreatedDate.Year, m.CreatedDate.Month },
m => m.TotalSatisfaction
)
.Select(g => new {
label = new DateTime(g.Key.Year, g.Key.Month, 1),
data = g.Sum()
})
.ToList();
but do say if it doesn't..
We group by a key of the year and month, and emit only the TotalSatisfaction from the group. Later we'll sum this up per yearmonth giving a list of distinct yearmonths and the total of their satisfactions
Solution 2:[2]
It looks like you're not actually moving each bullet in self.update(), just the most recent bullet that was created in self.shoot(). Try something like:
def update(self):
self.angle = self.angle % 360
self.xDir = math.radians(self.angle)
self.yDir = math.radians(self.angle)
for bullet in self.bullets:
bullet.x += math.cos(self.xDir) * 2.0
bullet.y -= math.sin(self.yDir) * 2.0
Also, I noticed you're doing "for self.bullet in self.bullets" in the self.drawToScreen() method, and that seems like that might cause issues (if it isn't already). Try doing just:
for bullet in self.bullets:
pygame.draw.rect(screen, (0, 255, 0), bullet)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | thisismy-stackoverflow |


