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 counts the amount of Leads and Sales. This table only creates when a new lead or sale is done, so some days there is no row inserted. Doing the following query works
var query3 = query2.AsEnumerable()
.Where(x => x.Created <= toDate.Value && x.Created >= fromDate.Value)
.GroupBy(x => x.Created.Date)
.Select(g => new ReferrerChart {
Date = g.Key.Date,
LeadsCount = g.Count(x => !x.IsSale),
SalesCount = g.Count(x => x.IsSale)
});
but some dates are not in there (the dates there were 0 leads and sales).
How can I include these dates and let them set LeadsCount = 0 and SalesCount = 0 WITHOUT using join?
EDIT:
Final result that works:
var selectedFromDate = fromDate.Value;
var selectedToDate = toDate.Value;
var selectedDates = Enumerable.Range(0, 1 + selectedToDate.Subtract(selectedFromDate).Days)
.Select(offset => new ReferrerChart {
Date = selectedFromDate.AddDays(offset),
LeadsCount = 0,
SalesCount = 0
})
.ToList();
var query3 = Find(x => x.Source == "UserRef").AsEnumerable()
.Where(x => x.Created <= toDate.Value && x.Created >= fromDate.Value)
.GroupBy(x => x.Created.Date)
.Select(g => new ReferrerChart {
Date = g.Key.Date,
LeadsCount = g.Count(x => !x.IsSale),
SalesCount = g.Count(x => x.IsSale)
}).ToList();
var result = query3.Union(
selectedDates
.Where(e => !query3.Select(x => x.Date).Contains(e.Date)))
.OrderBy(x => x.Date);
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…