我怎么能这样做?我认为问题在于“选择”中的连接。
var respaldos = db.Respaldoes.Where(x => x.type == "Backup")
.Where(x => x.start_time >= _desde && x.start_time <= _hasta)
.GroupBy(x => new
{
x.start_time.Year,
x.start_time.Month,
x.start_time.Day
})
.Select(x => new
{
periodo = x.Key.Day + "/" + x.Key.Month + "/" + x.Key.Year,
objetos = x.Sum(y => y.bytes_processed)
});
foreach (var obj in respaldos)
{
Dictionary<string, string> dicciconario = new Dictionary<string, string>();
dicciconario.Add("periodo", obj.periodo.ToString());
dicciconario.Add("objetos", obj.objetos.ToString());
}
运行应用时出错,错误发生在“foreach”
错误: No se pudo convertir el tipo'System.Int32'al tipo'System.Object'。 LINQ to Entitiessóloinlaladisteiónadtiposdeenumeraciónoprimitivos de EDM。
我尝试白衣:
.Select(x => new
{
periodo = x.Key.Day.ToString() + "/" + x.Key.Month.ToString() + "/" + x.Key.Year.ToString(),
objetos = x.Sum(y => y.bytes_processed)
});
我收到其他错误:
LINQ to Entities no reconoceelmétodoSystem.StringToString()delmétodo,yestemétodonose puede traducir enunaexpresióndealmacén。
答案 0 :(得分:1)
看起来您希望按照没有时间组件的日期进行分组。 EF具有以下功能:DbFunctions.TruncateTime:
var respaldos = db.Respaldoes.Where(x => x.type == "Backup")
.Where(x => x.start_time >= _desde && x.start_time <= _hasta)
.GroupBy(x => DbFunctions.TruncateTime(x.start_time))
.Select(x => new
{
periodo = x.Key, // Now a DateTime value
objetos = x.Sum(y => y.bytes_processed)
});
在此之后,您可以
Dictionary<DateTime, int> dicciconario =
respaldos.ToDictionary(x => x.periodo, x => x.objetos);