LINQ group by date - include empty days WITHOUT using join

后端 未结 3 769
天涯浪人
天涯浪人 2020-12-22 02:47

Using C#, NHibernate, NHibernate to LINQ. Using NHibernate to LINQ, I do not have the JOIN functionality. I can not use QueryOver either.

I have a LINQ query that co

3条回答
  •  没有蜡笔的小新
    2020-12-22 03:13

    You can Union() the data from your query with dummy items that you create in memory. For example:

    var query4 = query3.ToList(); // Prevent multiple execution.
    
    var startDate = DateTime.Today.AddDays(-99);
    
    var emptyData = Enumerable.Range(1,100).Select (i => 
        new ReferrerChart
            {
               Date = startDate.AddDays(i),
               LeadsCount = 0,
               SalesCount = 0
            });
    
    var result = query4 .Union(
                 emptyData
                    .Where(e => !query4.Select(x => x.Date).Contains(e.Date)))
                 .OrderBy(x => x.Date);
    

提交回复
热议问题