如何从循环中的列表中删除范围?

时间:2016-12-28 11:51:51

标签: c# list

问候请记住,没有数据库,这些都是伪造的功能,使组件可以进行测试,我有一个List,它根据24 hours比例制作15 minutes并使用此方法生成:

    public List<ScaleLine> GetHoursAndScales(ScaleLine.Scales scale = ScaleLine.Scales.Fiftheen)
    {
        int _scale = Convert.ToInt32(scale);
        int _count = _scale * 24;
        int _scaleCount = 60 / _scale;

        List<ScaleLine> _list = new List<ScaleLine>();

        var start = DateTime.Today;
        var clockQuery = from offset in Enumerable.Range(1, _count)
                         select TimeSpan.FromMinutes(_scaleCount * offset);
        foreach (var time in clockQuery)
        {
            _list.Add(new ScaleLine() { Id = _list.Count, Hours = (start + time).ToString("HH:mm"), Scale = _scale });
        }
        return _list;
    }

我还有另一个名为“保留时间”的列表,该列表是通过此方法生成的:

    public List<Reservedhours> AllReservedHours()
    {
        return new List<Reservedhours>
        {
            new Reservedhours() { Id = 1, Date = DateTime.Now, StartPoint = "08:00", EndPoint = "10:00" },
            new Reservedhours() { Id = 2, Date = DateTime.Now, StartPoint = "14:00", EndPoint = "16:00" },
            new Reservedhours() { Id = 3, Date = DateTime.Now, StartPoint = "20:00", EndPoint = "22:00" },
            new Reservedhours() { Id = 4, Date = DateTime.Now.AddDays(1), StartPoint = "07:00", EndPoint = "11:00" },
            new Reservedhours() { Id = 5, Date = DateTime.Now.AddDays(1), StartPoint = "13:00", EndPoint = "15:00" },
            new Reservedhours() { Id = 6, Date = DateTime.Now.AddDays(1), StartPoint = "15:00", EndPoint = "18:00" },
            new Reservedhours() { Id = 7, Date = DateTime.Now.AddDays(1), StartPoint = "18:00", EndPoint = "22:00" },

        }; 
    }

现在我有另一个列表,根据预留时间和第一个产生24小时的列表产生可用小时数:

    public List<ScaleLine> GetAvailableHours(DateTime date, ScaleLine.Scales scale = ScaleLine.Scales.Fiftheen)
    {
        List<Reservedhours> _timeLine = AllReservedHours().Where(x => x.Date.Date == date.Date)
            .Select( a => new Reservedhours {StartPoint = a.StartPoint, EndPoint = a.EndPoint } )
            .ToList();
        List<ScaleLine> _available = GetHoursAndScales();

        //scale convert:
        int _scale = Convert.ToInt32(scale);

        foreach (var _item in _timeLine)
        {
            int index = _available.Where(x => x.Hours == _item.StartPoint)
                .SingleOrDefault().Id;

            //Convert to datetime
            DateTime _opening = DateTime.ParseExact(_item.StartPoint, "HH:mm", System.Globalization.CultureInfo.InvariantCulture);
            DateTime _closing = DateTime.ParseExact(_item.EndPoint, "HH:mm", System.Globalization.CultureInfo.InvariantCulture);
            //Getting duration time
            TimeSpan duration = _closing.Subtract(_opening);
            double _duration = duration.TotalMinutes;
            //getting coverage
            int timeScale = 60 / _scale;
            int coverage = Convert.ToInt32(_duration) / timeScale;

            //remove unavailable timespots
            _available.RemoveRange(index, coverage);
        }
        return _available; 
    }

但问题是当Foreach循环开始时它会根据索引正确删除第一个范围,假设_available列表中有96个成员,它会删除8个它们,所以第二次它应该有88个成员并在index个成员中找到88但它没有并且索引错误(它将索引视为list仍然有96个成员,因此循环中的每个其他操作都是如此。我该如何解决这个问题?有没有办法可以在不进行foreach循环的情况下获得可用列表?

1 个答案:

答案 0 :(得分:1)

问题在于您对索引的确定。您不需要询问列表中所需对象的索引,而是询问对象的属性值并将其用作索引:

int index = _available.Where(x => x.Hours == _item.StartPoint)
                      .SingleOrDefault()
                      .Id;

要么通过调用IndexOf()来确切地询问索引:

var matchingItem = _available.Where(x => x.Hours == _item.StartPoint)
                             .First();
var index = _available.IndexOf(matchingItem);

或者您用其他内容替换.RemoveRange(),这实际上会删除您想要的元素。