Frage

var myDic = new SortedDictionary<DateTime,int> () 
                { { new DateTime(0), 0 },
                  { new DateTime(1), 1 },
                  { new DateTime(2), 1 },
                  { new DateTime(3), 0 },
                  { new DateTime(4), 0 },
                  { new DateTime(5), 2 }
                };

How can group these items (with a LINQ request) like this :

group 1 : 
  startDate: 0, endDate:0, value:0
group 2 : 
  startDate: 1, endDate:2, value:1
group 3 : 
  startDate: 3, endDate:4, value:0
group 4 : 
  startDate: 5, endDate:5, value:2

group are defined by contiguous date and same values.

Is it possible with a groupby ?

War es hilfreich?

Lösung

Just use a keyGenerating function. This example presumes your dates are already ordered in the source with no gaps.

int currentValue = 0;
int groupCounter = 0;

Func<KeyValuePair<DateTime, int>, int> keyGenerator = kvp =>
{
  if (kvp.Value != currentValue)
  {
    groupCounter += 1;
    currentValue = kvp.Value;
  }
  return groupCounter;
}

List<IGrouping<int, KeyValuePair<DateTime, int>> groups =
  myDictionary.GroupBy(keyGenerator).ToList();

Andere Tipps

It looks like you are trying to group sequential dates over changes in the value. I don't think you should use linq for the grouping. Instead you should use linq to order the dates and iterate over that sorted list to create your groups.

Addition 1 While you may be able to build your collections with by using .Aggregate(). I still think that is the wrong approach.

Does your data have to enter this function as a SortedDictionary? I'm just guessing, but these are probably records ordered chronologically.

If so, do this:

    public class Record
    {
        public DateTime Date { get; set; }
        public int Value { get; set; }
    }

    public class Grouper
    {
        public IEnumerable<IEnumerable<Record>> GroupRecords(IEnumerable<Record> sortedRecords)
        {
            var groupedRecords = new List<List<Record>>();
            var recordGroup = new List<Record>();
            groupedRecords.Add(recordGroup);

            foreach (var record in sortedRecords)
            {
                if (recordGroup.Count > 0 && recordGroup.First().Value != record.Value)
                {
                    recordGroup = new List<Record>();
                    groupedRecords.Add(recordGroup);
                }

                recordGroup.Add(record);
            }

            return groupedRecords;
        }
    }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top