我如何返回节日的每一天进行的阶段

  • 本文关键字:每一天 何返回 返回 c#
  • 更新时间 :
  • 英文 :


我是一名学生准备C#测试的学生,我有问题。也许由于我的英语弱,我无法正确地说它,所以我会跳到问题。

有一个持续几天的节日,并且有几个阶段,乐队可以同时表演。并非所有阶段都在节日的每一天都使用。这是结构:

public class Festival
{
    List<Performance> performances;
}
public class Performance
{
    DateTime start;
    string stage;
}

我需要编写一种可以返回节日每天使用的阶段(或阶段(的方法。

我用int day;char stage;制作了一个测试程序,这使我更容易玩。我将其与此代码一起使用:

List<Performance> allPerformances = new List<Performance>();
// Above I've added my test performances (1, 'A'), (1, 'B'), (2, 'A'), (2, 'C') etc.
List<int> allDaysNoRepetition = new List<int>();
List<char> allStagesNoRepetition = new List<char>();
foreach (Performance p in allPerformances)
{
    if (!allDaysNoRepetition.Contains(p.day))
    {
        allDaysNoRepetition.Add(p.day);
    }
    if (!allStagesNoRepetition.Contains(p.stage))
    {
        allStagesNoRepetition.Add(p.stage);
    }
}
foreach (char stage in allStagesNoRepetition)
{
    List<int> daysOnThisStage = new List<int>();
    foreach (int day in allDaysNoRepetition)
    {
        foreach (Performance p in allPerformances)
        {
            if (p.day == day && p.stage.Equals(stage) && !daysOnThisStage.Contains(day))
            { daysOnThisStage.Add(day); }
        }
    }
    if (daysOnThisStage.Count() == allDaysNoRepetition.Count())
    { Console.WriteLine(stage); }
}

这起作用了,但是我想知道除了在舞台上计算不同的日子和整体上的不同日子,然后进行比较,是否有一种更优雅的方法来制作这项工作?这是我应该使用的正确逻辑"在所有问题中发生"吗?

提前感谢,对不起,如果我还没有正确格式化,我是新秀。

正如我所理解的问题时,您只需要获得阶段,这些阶段每天都很忙。它可能不是很漂亮,但它是另一种方法:

Festival fest = new Festival();
    List<Performance> allPerformances = new List<Performance>();
    // Above I've added my test performances (1, 'A'), (1, 'B'), (2, 'A'), (2, 'C') etc.
    allPerformances.Add(new Performance(){day = DateTime.Parse("1/1/2019"), stage = "A"});
    allPerformances.Add(new Performance(){day = DateTime.Parse("1/1/2019"), stage = "B"});
    allPerformances.Add(new Performance(){day = DateTime.Parse("1/1/2019"), stage = "C"});
    allPerformances.Add(new Performance(){day = DateTime.Parse("1/2/2019"), stage = "A"});
    allPerformances.Add(new Performance(){day = DateTime.Parse("1/2/2019"), stage = "B"});
    allPerformances.Add(new Performance(){day = DateTime.Parse("1/2/2019"), stage = "C"});
    allPerformances.Add(new Performance(){day = DateTime.Parse("1/3/2019"), stage = "A"});
    allPerformances.Add(new Performance(){day = DateTime.Parse("1/3/2019"), stage = "C"});
    allPerformances.Add(new Performance(){day = DateTime.Parse("1/4/2019"), stage = "A"});
    allPerformances.Add(new Performance(){day = DateTime.Parse("1/4/2019"), stage = "C"});
    fest.performances = allPerformances.OrderBy(o=>o.day).ToList();
    //get list of ALL fest days
    var listOfDays = GetDateRange( fest.performances.Min(s=>(s.day)),fest.performances.Max(s=>(s.day)));
    List<string> result = new List<string>();
    allPerformances.GroupBy(g => new { g.stage })
    .ToList().ForEach(fe => { 
        if(listOfDays.Count() == fe.Select(s=>s.day).Distinct().Count())
            result.Add(fe.Select(s=>s.stage).FirstOrDefault());
    });
    Console.Write(result);

和类和功能

public class Festival
{
    public List<Performance> performances { get; set;}
    public Festival(){performances = new List<Performance>();}
}
public class Performance
{
    public DateTime day { get; set; }
    public string stage { get; set;}
    public Performance(){}
}
public static IEnumerable<DateTime> GetDateRange(DateTime startDate, DateTime endDate)
{
    if (endDate < startDate)
        throw new ArgumentException("endDate must be greater than or equal to startDate");
    while (startDate <= endDate)
    {
        yield return startDate;
        startDate = startDate.AddDays(1);
    }
}

结果,您应该得到A和C。我希望我正确理解你...

相关内容

最新更新