将记录分成单独的月份以用于 mvc

发布于 2024-09-16 04:07:59 字数 2247 浏览 1 评论 0原文

我有一组记录。其中有两名拳击手、比赛日期、地点等...... 我想将它们分开几个月并将它们组合在一起。目前我有以下内容。这在一定程度上是有效的。寻找未来的比赛日期。即今年,遍历每个月 (1-12) 并查找该日期范围内的任何匹配项。

将其放入 int, enumerable 的漂亮字典中,其中 int 是月份,enumberable 是该月的匹配项集合

//Build the matches list by Months!!!
var summarysDic = new Dictionary<int, IEnumerable<MatchSummary>>();
for (int i = 1; i <= 12; i++)
{
    var MatchesOfMonth = matches.Where(x => x.MatchDate.Value.Year == DateTime.Now.Year &&
                        x.MatchDate.Value.Month == i &&
                        !x.HasResult() &&
                        x.MatchDate.Value > DateTime.Now);
    if (MatchesOfMonth.Count() > 0)
    {
        summarysDic.Add(i, MatchesOfMonth.OrderBy(x => x.MatchDate).Select(x=> new MatchSummary(x)).ToArray());
    }
}

问题是目前仅处理今年。我想让它在“接下来的 6 个月”有效,但这当然也必须在新的一年有效!

执行此操作的最佳/最干净方法是什么?

提前致谢!

PS 在旁注中,我还没有找到如何简单地执行 DateTime.Now.Month.add(1) 例如(因为我将始终从当前日期向前移动!)

-----完成的代码!--- --

//Build the matches list by Months!!!
        var summarysDic = new Dictionary<string, IEnumerable<MatchSummary>>();
        for (int i = 1; i <= 12; i++)
        {
            var checkDate = DateTime.Now.AddMonths(i);
            var MatchesOfMonth = matches.Where(x => x.MatchDate.Value.Month == checkDate.Month &&
                                x.MatchDate.Value.Year == checkDate.Year &&
                                !x.HasResult() &&
                                x.MatchDate.Value > DateTime.Now);
            if (MatchesOfMonth.Count() > 0)
            {
                var firstMatchDate = MatchesOfMonth.First().MatchDate.Value;
                if (firstMatchDate.Year != DateTime.Now.Year)
                {
                    summarysDic.Add(firstMatchDate.ToString("MMMM yyyy"), MatchesOfMonth.OrderBy(x => x.MatchDate).Select(x => new MatchSummary(x)).ToArray());
                }
                else
                {
                    summarysDic.Add(firstMatchDate.ToString("MMMM"), MatchesOfMonth.OrderBy(x => x.MatchDate).Select(x => new MatchSummary(x)).ToArray());
                }

            }
        }

I have a collection of records. Which have two boxers, match date, location etc...
I want to separate them by months and group them together. Currently I have what is below. And it works to a degree. That looks for matchdates in the future. that is this year and steps through each month (1-12) and finds any matches in that date range.

Placing it into a nice dictionary of int, enumerable where int is the month and enumberable is the collection of matches in that month

//Build the matches list by Months!!!
var summarysDic = new Dictionary<int, IEnumerable<MatchSummary>>();
for (int i = 1; i <= 12; i++)
{
    var MatchesOfMonth = matches.Where(x => x.MatchDate.Value.Year == DateTime.Now.Year &&
                        x.MatchDate.Value.Month == i &&
                        !x.HasResult() &&
                        x.MatchDate.Value > DateTime.Now);
    if (MatchesOfMonth.Count() > 0)
    {
        summarysDic.Add(i, MatchesOfMonth.OrderBy(x => x.MatchDate).Select(x=> new MatchSummary(x)).ToArray());
    }
}

Problem is this currently only deals with this year. I would like to instead make it so it works for "the next 6 months" but this would of course have to work over the new year as well!

Whats the best/cleanest way to go about doing this?

thanks in advance!

P.S on a side note i have yet to find how to simply do DateTime.Now.Month.add(1) for example (as i will always be going from current date forwards!)

-----COMPLETED CODE!-----

//Build the matches list by Months!!!
        var summarysDic = new Dictionary<string, IEnumerable<MatchSummary>>();
        for (int i = 1; i <= 12; i++)
        {
            var checkDate = DateTime.Now.AddMonths(i);
            var MatchesOfMonth = matches.Where(x => x.MatchDate.Value.Month == checkDate.Month &&
                                x.MatchDate.Value.Year == checkDate.Year &&
                                !x.HasResult() &&
                                x.MatchDate.Value > DateTime.Now);
            if (MatchesOfMonth.Count() > 0)
            {
                var firstMatchDate = MatchesOfMonth.First().MatchDate.Value;
                if (firstMatchDate.Year != DateTime.Now.Year)
                {
                    summarysDic.Add(firstMatchDate.ToString("MMMM yyyy"), MatchesOfMonth.OrderBy(x => x.MatchDate).Select(x => new MatchSummary(x)).ToArray());
                }
                else
                {
                    summarysDic.Add(firstMatchDate.ToString("MMMM"), MatchesOfMonth.OrderBy(x => x.MatchDate).Select(x => new MatchSummary(x)).ToArray());
                }

            }
        }

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

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

发布评论

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

评论(2

私野 2024-09-23 04:07:59

我相信你不需要显着修改你的算法就能得到​​你想要的:

//Build the matches list by Months!!!
var summarysDic = new Dictionary<int, IEnumerable<MatchSummary>>();
for (int i = 0; i <= 6; i++)
{
    var checkDate = DateTime.Now.AddMonths(i);
    var MatchesOfMonth = matches.Where(x => x.MatchDate.Value.Year == checkDate.Year &&
                        x.MatchDate.Value.Month == checkDate.Month &&
                        !x.HasResult() &&
                        x.MatchDate.Value > DateTime.Now);
    if (MatchesOfMonth.Count() > 0)
    {
        summarysDic.Add(i, MatchesOfMonth.OrderBy(x => x.MatchDate).Select(x=> new MatchSummary(x)).ToArray());
    }
}

I believe you can get what you want without modifying your algorithm significantly:

//Build the matches list by Months!!!
var summarysDic = new Dictionary<int, IEnumerable<MatchSummary>>();
for (int i = 0; i <= 6; i++)
{
    var checkDate = DateTime.Now.AddMonths(i);
    var MatchesOfMonth = matches.Where(x => x.MatchDate.Value.Year == checkDate.Year &&
                        x.MatchDate.Value.Month == checkDate.Month &&
                        !x.HasResult() &&
                        x.MatchDate.Value > DateTime.Now);
    if (MatchesOfMonth.Count() > 0)
    {
        summarysDic.Add(i, MatchesOfMonth.OrderBy(x => x.MatchDate).Select(x=> new MatchSummary(x)).ToArray());
    }
}
轻许诺言 2024-09-23 04:07:59

DateTime.Now.AddMonth(1) 有什么问题吗?

var MatchesOfMonth = matches.Where(x => x.MatchDate.Value <= DateTime.Now.AddMonth(i)
                                   && !x.HasResult() 
                                   && x.MatchDate.Value > DateTime.Now);

我还没有编译它,但它应该只需要相当小的调整就可以运行......

What's wrong with DateTime.Now.AddMonth(1)?

var MatchesOfMonth = matches.Where(x => x.MatchDate.Value <= DateTime.Now.AddMonth(i)
                                   && !x.HasResult() 
                                   && x.MatchDate.Value > DateTime.Now);

I haven't compiled that, but it should run with only fairly minor tweeking...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文