Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
public IEnumerable<LoanReportViewModel> GetGroupByLoansOnDailyBasis(DateTime? StartDate, DateTime? EndDate)
        {
            var circulationLoans = _circulation.All().Where(l => l.CreatedDate >= StartDate && l.CreatedDate <= EndDate).ToList()
                .GroupBy(c => new DateTime(c.CreatedDate.Year, c.CreatedDate.Month, 1)).ToDictionary(g => g.Key.Date, g => g.Count());
            
            return circulationLoans.Select(g => new LoanReportViewModel()

            {
                LoanCreatedDate = g.Key.ToString(),
                LoansCount = g.Value
            });

        }



i want to see the result like
Date = 01/06/2015, Count=2
Date = 02/06/2015, Count=3
Date = 03/06/2015, Count=10
Date = 04/06/2015, Count=5


but i am Getting only The Loan Date as
Date = 01/06/2015 and total count =20 because it is looping all the counts Inside GroupBy Function() and sum it up at the end and than it populates the Key and Value Count to Query Result. i also want to Group these Results by Weekly and Monthly Basis From the Date Ranges.
Posted
Updated 15-Jun-15 23:01pm
v4
Comments
Timberbird 16-Jun-15 6:46am    
Excuse me, but how do you want to see the results like "Date = 01/06/2015, Date = 02/06/2015, Date = 03/06/2015" and so on if you group loans by year and month, not year, month and day? .GroupBy(c => new DateTime(c.CreatedDate.Year, c.CreatedDate.Month, 1)) effectively groups your loans using "First day of loan's month" as a key, so loans made on 01/06/2015, 02/06/2015 and 03/06/2015 will fall under the same group with "01/06/2015" key
Qasim Sarwar 16-Jun-15 7:58am    
Actually I am using code first approach in mvc I need to population through view model to pass results to crystal report so i am Assigning like this

var loanRecords = from records in _circulation.All().Where(x => x.CreatedDate >= StartDate && x.CreatedDate <= EndDate).ToList()
group records by records.CreatedDate into groupRecords
select new LoanReportViewModel
{
LoanCreatedDate = groupRecords.First().CreatedDate,
LoansCount = groupRecords.Count(),
};
return Resuts;

Still i am geting the Same Count as a total count after Adding Watch to loadRecords.

This is My ViewModel

public class LoanReportViewModel
{
public int LoansCount { get; set; }
public string LoanCreatedDate { get; set; }

}

var loanRecords = from records in _circulation.Where(x => x.CreatedDate.Date >= x.StartDate.Date && x.CreatedDate.Date <= EndDate.Date)
group records by records.CreatedDate.Date into groupRecords
select new
{
LoanDate = groupRecords.First().CreatedDate.Date,
LoanCount = groupRecords.Count()
}.ToList();
 
Share this answer
 
Try this:
C#
return _circulation.All()
    .Where(l => l.CreatedDate >= StartDate && l.CreatedDate <= EndDate)
    .GroupBy(c => c.CreatedDateToString("yyyy/MM/dd"))
    .Select(g => new LoanReportViewModel()
        {
            LoanCreatedDate = g.Key,
            LoansCount = g.Count()
        }).ToList();
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900