Linq查询从开始日期和结束日期明智地获取Group By Date。 [英] Linq Query to get Group By Date wise From Start and End Dates.

查看:85
本文介绍了Linq查询从开始日期和结束日期明智地获取Group By Date。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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希望看到结果如

日期= 01/06/2015,计数= 2

日期= 02/06/2015,计数= 3

日期= 03/06 / 2015,Count = 10

Date = 04/06/2015,Count = 5




但我只是贷款日期为

日期= 01/06/2015,总计数= 20 因为它正在循环所有计数内部 GroupBy函数()并在最后汇总,并将密钥和值计数填充到查询结果。我还想根据日期范围按周和每月基础对这些结果进行分组。

解决方案

var loanRecords =来自_circulation.Where中的记录(x => x。 CreatedDate.Date> = x.StartDate.Date && x.CreatedDate.Date< = EndDate.Date)

记录组记录.CreatedDate.Date到groupRecords

选择新的

{

LoanDate = groupRecords.First()。CreatedDate.Date,

LoanCount = groupRecords.Count()

} .ToList();


试试这个:

 返回 _circulation.All()
。其中(l = > l.CreatedDate > = StartDate&& l.CreatedDate < = EndDate)
.GroupBy(c = > c.CreatedDateToString( yyyy / MM / dd))
。选择(g = > new LoanReportViewModel()
{
LoanCreatedDate = g.Key,
LoansCount = g.Count()
})。ToList();


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.

解决方案

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();


Try this:

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();


这篇关于Linq查询从开始日期和结束日期明智地获取Group By Date。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆