C# LINQ分组依据,然后显示的日期时间(dd mmm)
我希望Linq按日期分组,但以文本显示 这是我的密码C# LINQ分组依据,然后显示的日期时间(dd mmm),c#,linq,entity-framework,datetime,group-by,C#,Linq,Entity Framework,Datetime,Group By,我希望Linq按日期分组,但以文本显示 这是我的密码 var groups = _uow.Orders.GetAll() .Where(x => x.Created > baselineDate) .GroupBy(x => x.Created.ToString("yyyy-MM-dd")); var orders = new { Day = groups.Select(g => g
var groups = _uow.Orders.GetAll()
.Where(x => x.Created > baselineDate)
.GroupBy(x => x.Created.ToString("yyyy-MM-dd"));
var orders = new
{
Day = groups.Select(g => g.Key).ToArray(),
Total = groups.Select(g => g.Sum(t => t.Total)).ToArray(),
};
结果是(不适合放入图形标签)
但是我想要这个(每月)
或者每天
"Day": ['Jan 1', 'Jan 2', 'Jan 3', 'Jan 4'], "Total": [9999.00, 9999.00, 9999.00, 9999.00] }
请为我提供可以玩的DateTime.ToString()的建议
谢谢大家。介绍如何将日期时间转换为月份的字符串名称
编辑:对于EF,在执行任何字符串逻辑之前,我们必须将所有内容都拉入内存:
var orders = _uow.Orders.GetAll()
.Where(x => x.Created > baselineDate)
// pull into memory here, since we won't be making the result set any smaller and
// we want to use DateTime.ToString(), which EF won't compile to SQL
.AsEnumerable()
// this will group by the whole date. If you only want to group by part of the date,
// (e. g. day or day, month), you could group by x => x.Date.Month or the like
.GroupBy(x => x.Date)
.Select(g => new {
// or use just "MMM" for just the month
Dates = g.Select(g => g.Key.ToString("MMM d")).ToArray(),
Total = ...
});
描述如何将日期时间转换为月份的字符串名称
编辑:对于EF,在执行任何字符串逻辑之前,我们必须将所有内容都拉入内存:
var orders = _uow.Orders.GetAll()
.Where(x => x.Created > baselineDate)
// pull into memory here, since we won't be making the result set any smaller and
// we want to use DateTime.ToString(), which EF won't compile to SQL
.AsEnumerable()
// this will group by the whole date. If you only want to group by part of the date,
// (e. g. day or day, month), you could group by x => x.Date.Month or the like
.GroupBy(x => x.Date)
.Select(g => new {
// or use just "MMM" for just the month
Dates = g.Select(g => g.Key.ToString("MMM d")).ToArray(),
Total = ...
});
您需要使用EF可以理解并转换为SQL的函数,例如。以下是您的更新代码:
var groups = _uow.Orders.GetAll()
.Where(x => x.Created > baselineDate)
.GroupBy(x => EntityFunctions.TruncateTime(x.Created));
然后仅在输出中使用字符串格式,但请确保在值返回到.Net后使用该格式,使用AsEnumerable
切换到Linq to Objects:
var orders = new
{
Day = groups.Select(g => g.Key)
.AsEnumerable()
.Select(x => x.ToString("MMM d"))
.ToArray(),
Total = groups.Select(g => g.Sum(t => t.Total)).ToArray(),
};
您需要使用EF可以理解并转换为SQL的函数,例如。以下是您的更新代码:
var groups = _uow.Orders.GetAll()
.Where(x => x.Created > baselineDate)
.GroupBy(x => EntityFunctions.TruncateTime(x.Created));
然后仅在输出中使用字符串格式,但请确保在值返回到.Net后使用该格式,使用AsEnumerable
切换到Linq to Objects:
var orders = new
{
Day = groups.Select(g => g.Key)
.AsEnumerable()
.Select(x => x.ToString("MMM d"))
.ToArray(),
Total = groups.Select(g => g.Sum(t => t.Total)).ToArray(),
};
谢谢大家的帮助。 我找到了答案 加 及 这是完整的代码
var groups = _uow.Orders.GetAll()
.Where(x => x.Created > baselineDate)
.AsEnumerable()
.GroupBy(x => x.Created.ToString("MMM d"));
var orders = new
{
Day = groups.Select(g => g.Key).ToArray(),
Total = groups.Select(g => g.Sum(t => t.Total)).ToArray(),
};
谢谢大家的帮助。 我找到了答案 加 及 这是完整的代码
var groups = _uow.Orders.GetAll()
.Where(x => x.Created > baselineDate)
.AsEnumerable()
.GroupBy(x => x.Created.ToString("MMM d"));
var orders = new
{
Day = groups.Select(g => g.Key).ToArray(),
Total = groups.Select(g => g.Sum(t => t.Total)).ToArray(),
};
是的,我知道如何使用ToString(“MMMM”)或任何其他stringFormat。但是,当我使用LINQ分组时,它不起作用。@riseres您使用LINQ分组对象吗?Linq到SQL?EF?NHibernate?非常感谢@但我的代码有误。g、 **选择**(g=>g.*Key**.ToString(“MMM d”)).ToArray()如何修复?它显示了此==>ExceptionMessage:“LINQ to Entities无法识别方法'System.String ToString(System.String)'方法,并且此方法无法转换为存储表达式。”@Risers:尝试我的更新代码。我已经添加了必要的AsEnumerable()是的,我知道如何使用ToString(“MMMM”)或任何其他stringFormat。但当我使用LINQ分组时,它不起作用。@riseres您使用LINQ到对象吗?LINQ到SQL?EF?NHibernate?非常感谢。@ChaseMedallion但我在代码中出错。g.*Select**(g=>g.*Key**.ToString(“MMM d”)。ToArray()如何修复它?它显示以下==>ExceptionMessage:“LINQ to Entities无法识别方法'System.String ToString(System.String)'方法,并且此方法无法转换为存储表达式。”@Risers:尝试我更新的代码。我已经为EF添加了必要的AsEnumerable()行