LINQ。按天分组。如何轻松做到这一点?

发布于 2024-09-06 02:57:27 字数 474 浏览 7 评论 0原文

我似乎找不到任何关于此的好的参考。我有很多带有日期的 SQL 数据。所以我想制作一个折线图来显示这些数据随时间的变化。如果我想在一段时间内显示它,那么我需要按天分组..但是LOGDATE是完整日期..而不是DAY..

所以我在下面有这个,但LINQ不知道什么是'DayOfYear'财产是...

 var q = from x in dc.ApplicationLogs
                let dt = x.LogDate
                group x by new { dayofyear = dt.Value.DayOfYear } into g
                select new
                {
                    iCount = g.Count(),
                    strDate = g.Key
                };

I can't seem to find any good reference on this. I have a lot of data in SQL with dates. So I wanted to make a line chart to show this data over time. If I want to show it over a period of days then I need to group by days.. But the LOGDATE is the full date.. not the DAY..

So I have this below, but LINQ doesn't know what 'DayOfYear' property is...

 var q = from x in dc.ApplicationLogs
                let dt = x.LogDate
                group x by new { dayofyear = dt.Value.DayOfYear } into g
                select new
                {
                    iCount = g.Count(),
                    strDate = g.Key
                };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

丑丑阿 2024-09-13 02:57:29

您希望 .Date 获取 DateTime 的日期部分而不是 DayOfyear 除非您故意尝试将每个日期放入一年中的同一天年入组。

You want .Date to get the date part of a DateTime not DayOfyear unless you are deliberately trying to put the same day of the year from each year into the group.

肤浅与狂妄 2024-09-13 02:57:29

你为什么使用:

    let dt = x.LogDate
    group x by new { dayofyear = dt.Value.DayOfYear } into g

而不是仅仅:

    group x by x.LogDate.Value.DayOfYear into g

我对此不确定,但是使用 group by 子句中的匿名对象可能会弄乱 L2S。

Why are you using:

    let dt = x.LogDate
    group x by new { dayofyear = dt.Value.DayOfYear } into g

instead of just:

    group x by x.LogDate.Value.DayOfYear into g

I'm not sure about this, but it's possible using an anonymous object like that in your group by clause is messing up L2S.

玉环 2024-09-13 02:57:29

干得好

let d = new DateTime(n.time.Year, n.time.Month, n.time.Day) group n by d into grp select grp

Here you go

let d = new DateTime(n.time.Year, n.time.Month, n.time.Day) group n by d into grp select grp
我的黑色迷你裙 2024-09-13 02:57:28

在 EF core 中,您可以使用 DateTime.Date 获取 DateTime 值的日期部分。

在 EF6 中,您可以使用 DbFunctions.TruncateTime:

返回给定日期并清除时间部分

var q = from x in dc.ApplicationLogs
        let dt = x.LogDate.Date
        // EF6:let dt = DbFunctions.TruncateTime(x.LogDate)
        group x by dt into g
        select new
        {
            iCount = g.Count(),
            strDate = g.Key
        };

In EF core you can use DateTime.Date to get the date portion of a DateTime value.

In EF6 you can use DbFunctions.TruncateTime:

to return the given date with the time portion cleared

var q = from x in dc.ApplicationLogs
        let dt = x.LogDate.Date
        // EF6:let dt = DbFunctions.TruncateTime(x.LogDate)
        group x by dt into g
        select new
        {
            iCount = g.Count(),
            strDate = g.Key
        };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文