使用C#,NHibernate,NHibernate到LINQ。使用NHibernate到LINQ,我没有JOIN功能。我也不能使用QueryOver。
我有一个LINQ查询,用于计算潜在客户和销售额。此表仅在新的销售线索或销售完成时创建,因此有些日期没有插入行。执行以下查询
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)
});
但有些日期不在那里(日期有0个线索和销售)。
如何包含这些日期并让他们设置LeadsCount = 0和SalesCount = 0而不使用连接?
编辑:
最终结果有效:
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);
答案 0 :(得分:4)
您可以使用在内存中创建的虚拟项目Union()
查询中的数据。例如:
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);
答案 1 :(得分:1)
我将在LINQ查询中更改两件事,我将包括空日期
.Where(x => x.Created <= toDate.Value && x.Created >= fromDate.Value)
变得类似
.Where(x => x.Created.Date == null || (x.Created <= toDate.Value && x.Created >= fromDate.Value))
我的另一个指针是你的.GroupBy需要一个x.Created.Date,使用IsNull或类似的函数将这里的值设置为一个值,无论条目是否为Null
E.g。 (x.Created == null ? "" : x.Created.ToString("yyyyMMdd"))
(我很抱歉,但我现在不在我的开发PC上,因此无法明确说明正确的代码)
答案 2 :(得分:0)
通过为IEnumerable<IGrouping<TKey, TElement>>
(从GroupBy
返回的类型)实现扩展方法,这是一种很好的方法。我认为,出于多种原因,这是最好的方法:
.ToList()
也可以实现这一点,但是以急切的评估为代价),GroupBy
,public static IEnumerable<IGrouping<TKey, TElement>> Fill<TKey, TElement>(this IEnumerable<IGrouping<TKey, TElement>> groups, IEnumerable<TKey> filling)
{
List<TKey> keys = filling.ToList();
foreach (var g in groups)
{
if(keys.Contains(g.Key))
keys.Remove(g.Key);
yield return g;
}
foreach(var k in keys)
yield return new EmptyGrouping<TKey, TElement>(k);
}
class EmptyGrouping<TKey, TElement> : List<TElement>, IGrouping<TKey, TElement>
{
public TKey Key { get; set; }
public EmptyGrouping(TKey key)
{
this.Key = key;
}
}
Random rand = new Random();
var results = Enumerable.Repeat(0, 5) // Give us five
.Select(i => rand.Next(100)) // Random numbers 0 - 99
.GroupBy(r => r.Dump("Calculating group for:") / 10) // Group by tens (0, 10, 20, 30, 40...)
.Fill(Enumerable.Range(0, 10)) // Fill aby missing tens
.OrderBy(g => g.Key); // Sort
@"Anything above = eager evaluations.
Anything below = lazy evaluations.".Dump();
results.Dump();
(在评估时,查询内部会打印出五个整数。正如您所看到的,只有一个计算通道。)