C# 为mvc将记录分为单独的月份

C# 为mvc将记录分为单独的月份,c#,.net,model-view-controller,C#,.net,Model View Controller,我有一套唱片。有两名拳击手,比赛日期,地点等。。。 我想几个月把它们分开,然后把它们组合在一起。目前我有下面的内容。它在一定程度上起作用。这会寻找未来的比赛日期。也就是今年,每个月(1-12)进行一步,并查找该日期范围内的任何匹配项 将其放入一个不错的int字典,其中int是月份,enumberable是该月份匹配项的集合 //Build the matches list by Months!!! var summarysDic = new Dictionary<int, IEnumera

我有一套唱片。有两名拳击手,比赛日期,地点等。。。 我想几个月把它们分开,然后把它们组合在一起。目前我有下面的内容。它在一定程度上起作用。这会寻找未来的比赛日期。也就是今年,每个月(1-12)进行一步,并查找该日期范围内的任何匹配项

将其放入一个不错的int字典,其中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());
    }
}
//按月建立匹配列表!!!
var summarysDic=新字典();
for(int i=1;i x.MatchDate.Value.Year==DateTime.Now.Year&&
x、 MatchDate.Value.Month==i&&
!x.HasResult()&&
x、 MatchDate.Value>DateTime.Now);
if(MatchesOfMonth.Count()>0)
{
添加(i,MatchesOfMonth.OrderBy(x=>x.MatchDate)。选择(x=>newmatchsummary(x)).ToArray();
}
}
问题是,目前这只涉及今年的交易。我想改为让它在“未来6个月”有效,但这当然也必须在新年期间有效

做这件事最好/最干净的方法是什么

提前谢谢

另一方面,我还没有找到如何简单地做DateTime.Now.Month.add(1)(因为我总是从当前日期开始!)

-----完成代码-----

//按月建立匹配列表!!!
var summarysDic=新字典();
对于(int i=1;i 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)。选择(x=>newmatchsummary(x)).ToArray();
}
其他的
{
summarysDic.Add(firstMatchDate.ToString(“MMMM”),MatchesOfMonth.OrderBy(x=>x.MatchDate)。选择(x=>newmatchsummary(x)).ToArray();
}
}
}

我相信您可以在不大幅修改算法的情况下获得所需:

//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());
    }
}
//按月建立匹配列表!!!
var summarysDic=新字典();
对于(int i=0;i x.MatchDate.Value.Year==checkDate.Year&&
x、 MatchDate.Value.Month==checkDate.Month&&
!x.HasResult()&&
x、 MatchDate.Value>DateTime.Now);
if(MatchesOfMonth.Count()>0)
{
添加(i,MatchesOfMonth.OrderBy(x=>x.MatchDate)。选择(x=>newmatchsummary(x)).ToArray();
}
}

我相信您可以在不大幅修改算法的情况下获得所需:

//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());
    }
}
//按月建立匹配列表!!!
var summarysDic=新字典();
对于(int i=0;i x.MatchDate.Value.Year==checkDate.Year&&
x、 MatchDate.Value.Month==checkDate.Month&&
!x.HasResult()&&
x、 MatchDate.Value>DateTime.Now);
if(MatchesOfMonth.Count()>0)
{
添加(i,MatchesOfMonth.OrderBy(x=>x.MatchDate)。选择(x=>newmatchsummary(x)).ToArray();
}
}

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

var MatchesOfMonth=matches.Where(x=>x.MatchDate.Value DateTime.Now);

我还没有编译它,但它应该只运行很小的tweeking…

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

var MatchesOfMonth=matches.Where(x=>x.MatchDate.Value DateTime.Now);

我还没有编译它,但它应该只运行相当小的tweeking…

这会导致问题,因为我希望将8月份的所有比赛分组,并将9月份的所有比赛分组。我不希望将8月15日至9月15日分组,这会导致问题,因为我希望将8月的所有比赛分组,并将9月的所有比赛分组。我不想将8月15日至9月15日分组完美知道有一个简单的方法:)我也扩展了它,所以用一个字符串替换“int”,该字符串将在页面上用作每个组的标题,如果今年是一个月(9月),如果另一年是一个月(2012年9月)perfect知道有一个简单的方法:)我也扩展了它,所以用一个字符串替换“int”,该字符串将在页面上用作每个组的标题,如果今年是一个月(9月),或者如果另一年“2012年9月”看起来不错,则只需一个月(9月),Steve,不过有一件事,您可能希望将for循环修改为“for”(inti=0;我现在看起来不错,不过有一件事,您可能希望将for循环修改为“for(inti=0;i
var MatchesOfMonth = matches.Where(x => x.MatchDate.Value <= DateTime.Now.AddMonth(i)
                                   && !x.HasResult() 
                                   && x.MatchDate.Value > DateTime.Now);