开发者

C# start with certain value in foreach

开发者 https://www.devze.com 2023-02-12 23:49 出处:网络
Is there a way to start with a certain value in the foreach? In specific I would like to start with 7:30AM.

Is there a way to start with a certain value in the foreach? In specific I would like to start with 7:30AM.

DateTime start = DateTime.Today; 
var clock = from offset in Enumerable.Range(0, 48)
                 s开发者_如何学Pythonelect start.AddMinutes(30 * offset);
foreach (DateTime time in clock)
{
    string timeValue = time.ToString("hh:mmtt");
}


In short, no. Just make your Enumerable.Range cover the right range (might want to double-check):

var clock = from offset in Enumerable.Range(15, 48)
                 select start.AddMinutes(30 * offset);

Edit

A cleaner solution might be something like:

DateTime start = // set start
DateTime end = // set end
while (start <= end) {
    // do stuff here
    start = start.AddMinutes(30);
}


It is not possible to "start" at a different value in the collection with the foreach syntax directly (wrapping is an option and the following example could even be considered a form of wrapping -- LINQ generation is often lazy), but it is possible to iterate over an appropriately modified collection and obtain the desired semantics.

Enumerable.Range returns something IEnumerable<T> -- you can always Where-limit or Skip-through it (e.g., see the IEnumerable/LINQ (extension) methods) and then use the resultant as the "for each" collection.

For instance (this relies on the fact that time is increasing and somewhat "inefficient", but chances of it even mattering are close to nil in most applications):

foreach (DateTime time in clock.Where(t => t >= someStartTime)) {
    string timeValue = time.ToString("hh:mmtt");
}

Another solution based on ide's comment (which is, in my mind, cleaner than the above -- it also has to evaluate the conditional less):

foreach (DateTime time in clock.SkipWhile(t => t < someStartTime)) {
    ...
}

Note that these IEnumerable results are lazily evaluated streams.

However, some of the other answers provide nice alternative solutions and different ways of approaching the problem. I would only use the above if the initial sequence could not be altered and/or was used for different purposes requiring the original data.

Happy coding.


You can set your start to 7:30AM today, like this:

DateTime start = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 7, 30, 0);


Just replace this, It'll start with 7:30 AM.

var clock = from offset in Enumerable.Range(15, 48)

Good luck!


DateTime start = DateTime.Today.Add(new TimeSpan(7, 30, 0)); 

var clock = from offset in Enumerable.Range(0, 48)
                 select start.AddMinutes(30 * offset);
foreach (DateTime time in clock)
{
    string timeValue = time.ToString("hh:mmtt");
}

So, to start from 7:30 today, the key line is the first one:

DateTime start = DateTime.Today.Add(new TimeSpan(7, 30, 0)); 


It would be clearest is you set the time specifically and add to that. By simply changing the starting point of the enumeration, you'll get the desired starting times but it is unclear what the value is by inspection.

var start = DateTime.Today.Add(new TimeSpan(7, 30, 0));
var times = Enumerable.Range(0, 48)
                      .Select(offset => start.AddMinutes(30 * offset));
foreach (var time in times)
{
    string timeValue = time.ToString("hh:mmtt");
    // ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消