3

Let's say I have a List<Type> and a Type with the properties:

public DateTime OriginalDateTime { get; set; }
public double RoundedTime { get; }
public int MyValue { get; set; }

And that class contains a constructor:

public Match(DateTimeOffset dateTime, int bid, int ask, int volume)
{
            DateTime = dateTime;
            RoundedTime = Math.Round(Math.Ceiling(dateTime.ToTimeStamp() / 60) * 60); // Round to nearest minute
            Bid = bid;
            Ask = ask;
            Volume = volume;
}

Now using LINQ and GroupBy I get all the results grouped by RoundedTime. No problem, however, I want to check if there is a result for every minute (so for an hour of data, the GroupBy should return 60 results).

Preferably, when a minute is missing from the LINQ result, I'd like to use the previous value (let's say minute 30 is missing, fill it with the data of minute 29).

Now I'm wondering if this is even possible with LINQ.

2
  • 2
    Yes, e.g. you could use an Enumerable.Range to generate the 0-60 sequence and for each one look up the corresponding value or max value <= current as a mapping. But you might do better writing a yield function to walk the (sorted) list and fill in the gaps.
    – Rup
    Commented Apr 5, 2018 at 12:04
  • 2
    TakeWhile() where minute < 30 (or whatever), then LastOrDefault.
    – mjwills
    Commented Apr 5, 2018 at 12:46

1 Answer 1

3

I guess you want to achieve this:

IEnumerable<IGrouping<int, MyType>> mygrouping = new List<MyType>().GroupBy(x => x.Key); // this is your grouped list

var allData = Enumerable.Range(0, 60) // here you take 60 values (0-59)
            .Select(
              actualKey =>
              {
                  var p = mygrouping.SingleOrDefault(y => y.Key == actualKey);
                  int counter = actualKey;
                  while (p == null && counter >= 0) // as long as the value is null and the counter is greater or equal to 0
                      p = mygrouping.SingleOrDefault(y => y.Key == counter--);
                  return new KeyValuePair<int, IEnumerable<MyType>>(actualKey, p.Select(v => v)); // return the data of the last value
              });

this is just a little help, you need to modify the code to make it work with yours I guess. AND: there are more performant ways I guess

4
  • Looks great. Since performance is somewhat important, I guess it's better to write a method instead of a 'single' LINQ query?
    – Viletung
    Commented Apr 5, 2018 at 12:42
  • @Viletung yeah, for debugging-purposes and readability I guess it's better to write a function than a single linq. Commented Apr 5, 2018 at 12:43
  • also for performance right, or is LINQ faster? Perhaps I should do some research.
    – Viletung
    Commented Apr 5, 2018 at 12:45
  • 1
    plus, there is a while (p == null && counter >= 0) ... in. maybe you find a better variant, when you are in a function, you can store the last key you found in your grouping. then you can take that instead of iterating back with that while. - then this is much better for performance, too. Commented Apr 5, 2018 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.