我写了如下代码:
ViewBag.result = allStoreItems.Union(Enumerable.Range(1, 30)
.Select(offset => new StoreAnalyticOrders { OrderDate = DateTime.Now.AddDays(-(30)).AddDays(offset), AmountPaid = 0 }))
.SelectMany(x => x.StoreItemTransactions)
.GroupBy(x => x.TransactionDate.Value.Date)
.Select(cl => new StoreAnalyticOrders {
OrderDate = cl.Key,
AmountPaid = cl.Sum(c => c.TransactionPrice)
})
.OrderBy(x => x.OrderDate.Date)
.ToList();
基本上,这段代码的作用是将数据库中的可用日期分组,并对分组天数的值进行求和。
请注意以下部分代码:
.Union(Enumerable.Range(1, 30)
.Select(offset => new StoreItemTransactions { TransactionDate = DateTime.Now.AddDays(-(30)).AddDays(offset), TransactionPrice = 0 }))
我试图添加那些在我的数据库中不存在的缺失日期。例如,如果我'有这样的日期:
Date Amount
21 Feb. 2017 (some amount here)
19 Feb. 2017
13 Feb. 2017
现在输出将是缺失的日子+从今天开始的日期(2017年2月21日);
输出应为:
Date Amount
21 Feb. 2017 (some amounts here)
20 Feb. 2017
19 Feb. 2017
18 Feb. 2017
17 Feb. 2017
16 Feb. 2017
15 Feb. 2017
14 Feb. 2017
13 Feb. 2017
12 Feb. 2017
11 Feb. 2017
10 Feb. 2017
... all the way back to 21 Feb. -30 days
如您所见,列表中不存在的日期被指定为0 ...
除此之外,allStoreItems是一个StoreItems类型的列表,你可以看到我正在创建一个新类型(我在视图中显示这两个属性的自定义类;
我来这里的错误是:
List<StoreItems> does not contain a definition for 'Union' and the best extension method overload Queryable.Union<StoreAnalyticOrders>(IQueryable<StoreAnalyticOrders>) requires a receiver of Type IQueryable<StoreAnalyticOrders>
我做错了什么,我该如何解决?
答案 0 :(得分:1)
如果你从
开始 allStoreItems.Union(...)
然后需要与allStoreItems的类型相同,因为它不是,你得到你的错误。因此,您需要将工会移至以后。考虑这个的最简单方法是构建两个列表然后将它们组合起来。
假设您有列表
var list1 = allStoreItems
.SelectMany(x => x.StoreItemTransactions)
.GroupBy(x => x.TransactionDate.Value.Date)
.Select(cl => new StoreAnalyticOrders {
OrderDate = cl.Key,
AmountPaid = cl.Sum(c => c.TransactionPrice)
})
.OrderBy(x => x.OrderDate.Date)
.ToList();
假设上述情况有效,并为您提供List<StoreAnalyticOrders>
,那么您可以通过
var list2 = Enumerable.Range(1, 30)
.Select(offset => new StoreAnalyticOrders { OrderDate = DateTime.UtcNow.Date.AddDays(-(30)).AddDays(offset), AmountPaid = 0 }).ToList();
然后,您可以使用Union组合这些列表。但是,您需要创建一个方法来指示您只想比较OrderDate并忽略AmountPaid
这可能类似于
public class StoreAnalyticOrdersComparer : IEqualityComparer<StoreAnalyticOrders>
{
bool IEqualityComparer<StoreAnalyticOrders>.Equals(StoreAnalyticOrders s1, StoreAnalyticOrders s2)
{
return s1.OrderDate == s2.OrderDate;
}
int IEqualityComparer<StoreAnalyticOrders>.GetHashCode(StoreAnalyticOrders obj)
{
if (Object.ReferenceEquals(obj, null))
return 0;
return obj.OrderDate.GetHashCode();
}
}
这可以像
一样使用ViewBag.result = list1.Union(list2, new StoreAnalyticOrdersComparer())
.OrderBy(a=>a.OrderDate).ToList();