I need little help with the three functions below. I expect the functions to take the records daily, monthly and all records of the current year. However, I notice on the daily report the amount of 'scrap' is around 126 meanwhile monthly and year reports are showing 32.
Why 126 'scrap' in the daily report is not included in the others reports? Thank you in advance.
public async Task<List<Scrap>> GetDailyScrap()
{
return await _dbContext.Scrap.Where(x =>
x.Created.Year == DateTime.Now.Year &&
x.Created.Month == DateTime.Now.Month &&
x.Created.Day == DateTime.Now.Day).ToListAsync();
}
public async Task<List<Scrap>> GetMonthlyScrap()
{
return await _dbContext.Scrap.Where(x =>
x.Created.Year == DateTime.Now.Year &&
x.Created.Month == DateTime.Now.Month).ToListAsync();
}
public async Task<List<Scrap>> GetYearScrap()
{
return await _dbContext.Scrap.Where(x =>
x.Created.Year == DateTime.Now.Year).ToListAsync();
}

The amount of scrap for KST-420(daily chart) to reflect with the correct numbers on the monthly and year report.
- Scrap Model :
public class Scrap
{
public int Id { get; set; }
public int ScrapLineId { get; set; }
public string Line { get; set; }
public string Type { get; set; }
public string Position { get; set; }
public string Tag { get; set; }
public int Shift { get; set; }
public int ShiftLeaderPersonalId { get; set; }
public int OperatorPersonalId { get; set; }
public int Quantity { get; set; }
public int Week { get; set; }
public DateTime Created { get; set; }
}
endpoint:
//Daily Bar Chart SCRAP
List<Scrap> dailyScrap = await _scrapService.GetDailyScrap();
List<string> xValues = dailyScrap.DistinctBy(x => x.Line).Select(x => x.Line).ToList();
List<int> yValues = dailyScrap.DistinctBy(x => x.Quantity).Select(x => x.Quantity).ToList();
// Monthly Bar Chart SCRAP
List<Scrap> monthlyScrap = await _scrapService.GetMonthlyScrap();
List<string> xValuesMonthly = monthlyScrap.DistinctBy(x => x.Line).Select(x => x.Line).ToList();
List<int> yValuesMonthly = monthlyScrap.DistinctBy(x => x.Quantity).Select(x => x.Quantity).ToList();
// Year Bar Chart SCRAP
List<Scrap> yearScrap = await _scrapService.GetYearScrap();
List<string> xValuesYear = yearScrap.DistinctBy(x => x.Line).Select(x => x.Line).ToList();
List<int> yValuesYear= yearScrap.DistinctBy(x => x.Quantity).Select(x => x.Quantity).ToList();
DateTime.Now.*values with hardcoded numbers? I'm asking this because EF will in some cases attempt to interpret your code directly instead of using the underlying value. Another test you can do, is create a variable for those values, and use the variables in the query. See if that makes a difference in the results.IQueryablein the debugger.YEAR(created)=2023 AND MONTH(created)....that can't use any indexes. Use a range condition instead, eg.Where(x => x.Created>=DateTime.Today && x.Created <DateTime.Today.AddDays(1)). The resulting query will use a simple range condition, egcreated >='2023-01-17 AND created < '2024-01-18'