Given class
public class SomeType
{
public string Name;
public string Field2;
public DateTime CreatedOnDateTime
}
I want to take a List<SomeType> and divide it into several List<SomeType> where each List contains items with an equal CreatedOnDateTime stamp. In many cases CreatedOnDateTime will be the same, it would be nice to allow a tolerance of a couple seconds.
I could run a LINQ query N number of times to create each distinct list. Is there a more efficient mechanism? In other words, can this type of query be built with LINQ using some type of grouping mechanism? (when I say grouping I am imagining a RegEx)
Why would you use a regex? That’s for textual pattern matching. It sounds like you want
ToLookup:Note that that will go for identical timestamps. Creating a “tolerance” as such is difficult, but you could effectively “round down” the entries to a couple of seconds:
(Think of a better name if you want 🙂
The reason simple “tolerance” is hard is this situation:
Entries 1 and 2 are only a second apart… so they should go in the same bucket. But entries 2 and 3 are only a second apart… so they should go in the same bucket too. Entries 3 and 4 are only a second apart, so entry 4 should go in the same bucket as well. Now we’ve got entries 1 and 4, three seconds apart, in the same bucket.