使用嵌套字典编写此类条目的最佳、更实用的方法是什么?使用哪种设计模式? C#
What is the best, more practical way to write such an entry with nested dictionaries? Which design pattern to use? C#
数据库中数据较多,需要使用c#collections进行统计(求应用程序每个用户每天平均操作次数)。在我看来,有必要使用字典:
var dict = new Dictionary<long?, Dictionary<DateTime, Dictionary<OperationsGroupType, int>>>();
请指教一种更实用的写法。因为它看起来很奇怪。谢谢
我写了一个函数:
public void D()
{
var dict = new Dictionary<long?, Dictionary<DateTime, Dictionary<OperationsGroupType, int>>>();
int pageNumber = 0;
int pageSize = 5;
int pageCount = 1;
while (pageNumber < pageCount)
{
int count;
foreach (OperationData op in OperationService.GetPage(pageNumber, pageSize, out count))
if(op.PerformedBy.HasValue)
if(op.PerformedDate.HasValue)
if (dict.ContainsKey(op.PerformedBy))
if (dict[op.PerformedBy].ContainsKey(op.PerformedDate.Value.Date.Date))
if (dict[op.PerformedBy][op.PerformedDate.Value.Date.Date.Date.Date].ContainsKey(op.Type)) dict[op.PerformedBy][op.PerformedDate.Value.Date.Date.Date.Date][op.Type]++;
else dict[op.PerformedBy][op.PerformedDate.Value.Date.Date.Date.Date].Add(op.Type, 1);
else dict[op.PerformedBy].Add(op.PerformedDate.Value.Date.Date.Date.Date, new Dictionary<OperationsGroupType, int> { { op.Type, 1 } });
else dict.Add(op.PerformedBy, new Dictionary<DateTime, Dictionary<OperationsGroupType, int>> { { op.PerformedDate.Value.Date.Date.Date.Date, new Dictionary<OperationsGroupType, int> { { op.Type, 1 } } } });
pageCount = (count - 1) / pageSize + 1;
pageNumber++;
}
foreach (var item in dict)
{
var opDateDict = new Dictionary<DateTime, int>();
foreach (var operDate in item.Value) opDateDict.Add(operDate.Key, operDate.Value.Sum(count => count.Value));
SystemLogger.Instance.WriteErrorTrace(String.Format("Average number of user operations {0} per day: {1}\n", item.Key, opDateDict.Values.Sum() / opDateDict.Count));
}
}
OperationsGroupType - this enum
请教一下如何更换字典更实用的设计?
哪种模式最适合解决这个问题?
很难说什么是最好的或最实用的 - 那是因为您没有真正定义“最佳”或“实用”的含义。
我将它们定义为最少的代码和最少的重复。
首先,我创建了这些扩展方法:
public static class Ex
{
public static R Ensure<T, R>(this Dictionary<T, R> @this, T key) where R : new
{
if (@this.ContainsKey(key))
return @this[key];
else
{
var r = new R();
@this[key] = r;
return r;
}
}
public static R Ensure<T, R>(this Dictionary<T, R> @this, T key, Func<R> factory)
{
if (@this.ContainsKey(key))
return @this[key];
else
{
var r = factory();
@this[key] = r;
return r;
}
}
}
有了这些我可以像这样重写你的代码:
foreach (OperationData op in OperationService.GetPage(pageNumber, pageSize, out count))
{
if (op.PerformedBy.HasValue)
if (op.PerformedDate.HasValue)
{
dict.Ensure(op.PerformedBy).Ensure(op.PerformedDate.Value.Date).Ensure(op.Type, () => 0);
dict[op.PerformedBy][op.PerformedDate.Value.Date][op.Type]++;
}
}
数据库中数据较多,需要使用c#collections进行统计(求应用程序每个用户每天平均操作次数)。在我看来,有必要使用字典:
var dict = new Dictionary<long?, Dictionary<DateTime, Dictionary<OperationsGroupType, int>>>();
请指教一种更实用的写法。因为它看起来很奇怪。谢谢
我写了一个函数:
public void D()
{
var dict = new Dictionary<long?, Dictionary<DateTime, Dictionary<OperationsGroupType, int>>>();
int pageNumber = 0;
int pageSize = 5;
int pageCount = 1;
while (pageNumber < pageCount)
{
int count;
foreach (OperationData op in OperationService.GetPage(pageNumber, pageSize, out count))
if(op.PerformedBy.HasValue)
if(op.PerformedDate.HasValue)
if (dict.ContainsKey(op.PerformedBy))
if (dict[op.PerformedBy].ContainsKey(op.PerformedDate.Value.Date.Date))
if (dict[op.PerformedBy][op.PerformedDate.Value.Date.Date.Date.Date].ContainsKey(op.Type)) dict[op.PerformedBy][op.PerformedDate.Value.Date.Date.Date.Date][op.Type]++;
else dict[op.PerformedBy][op.PerformedDate.Value.Date.Date.Date.Date].Add(op.Type, 1);
else dict[op.PerformedBy].Add(op.PerformedDate.Value.Date.Date.Date.Date, new Dictionary<OperationsGroupType, int> { { op.Type, 1 } });
else dict.Add(op.PerformedBy, new Dictionary<DateTime, Dictionary<OperationsGroupType, int>> { { op.PerformedDate.Value.Date.Date.Date.Date, new Dictionary<OperationsGroupType, int> { { op.Type, 1 } } } });
pageCount = (count - 1) / pageSize + 1;
pageNumber++;
}
foreach (var item in dict)
{
var opDateDict = new Dictionary<DateTime, int>();
foreach (var operDate in item.Value) opDateDict.Add(operDate.Key, operDate.Value.Sum(count => count.Value));
SystemLogger.Instance.WriteErrorTrace(String.Format("Average number of user operations {0} per day: {1}\n", item.Key, opDateDict.Values.Sum() / opDateDict.Count));
}
}
OperationsGroupType - this enum
请教一下如何更换字典更实用的设计? 哪种模式最适合解决这个问题?
很难说什么是最好的或最实用的 - 那是因为您没有真正定义“最佳”或“实用”的含义。
我将它们定义为最少的代码和最少的重复。
首先,我创建了这些扩展方法:
public static class Ex
{
public static R Ensure<T, R>(this Dictionary<T, R> @this, T key) where R : new
{
if (@this.ContainsKey(key))
return @this[key];
else
{
var r = new R();
@this[key] = r;
return r;
}
}
public static R Ensure<T, R>(this Dictionary<T, R> @this, T key, Func<R> factory)
{
if (@this.ContainsKey(key))
return @this[key];
else
{
var r = factory();
@this[key] = r;
return r;
}
}
}
有了这些我可以像这样重写你的代码:
foreach (OperationData op in OperationService.GetPage(pageNumber, pageSize, out count))
{
if (op.PerformedBy.HasValue)
if (op.PerformedDate.HasValue)
{
dict.Ensure(op.PerformedBy).Ensure(op.PerformedDate.Value.Date).Ensure(op.Type, () => 0);
dict[op.PerformedBy][op.PerformedDate.Value.Date][op.Type]++;
}
}