public static MemoryStream ChunkToCsv(IEnumerable <EventData> es)
        {
            es = es.ToList();
            var plumbing = new Plumbing(es.Count());
            var i        = 0;

            foreach (var e in es)
            {
                ParseRawFlatEvent(i++, plumbing, e, e.Payload);
            }
            var headerKeys = plumbing.Headers.Keys.ToArray();
            var csvStream  = ToCsvStream(headerKeys, new ArraySegment <SortedList <string, Node> >(plumbing.AlmostCsvs, 0, i));

            plumbing.Headers.Clear();
            return(csvStream);
        }
        public static SortedList <string, Node> ParseRawFlatEvent(int i, Plumbing plumbing, EventData data, Stream rawStream)
        {
            var res     = plumbing.AlmostCsvs[i];
            var headers = plumbing.Headers;

            FillSystemDates(data, headers, res);
            using (var streamReader = new StreamReader(rawStream))
                using (var reader = new JsonTextReader(streamReader)
                {
                    DateParseHandling = DateParseHandling.None
                })
                {
                    while (reader.Read())
                    {
                        if (reader.TokenType != JsonToken.PropertyName)
                        {
                            continue;
                        }
                        var prop = reader.Path;
                        headers[prop] = true;
                        reader.Read();
                        var  value = reader.TokenType == JsonToken.StartArray ? ReadArrayAsString(reader) : reader.Value;
                        Node node;
                        if (res.TryGetValue(prop, out node))
                        {
                            node.UseMe   = true;
                            node.Payload = value;
                        }
                        else
                        {
                            res.Add(prop, new Node {
                                UseMe = true, Payload = value
                            });
                        }
                    }
                }
            return(res);
        }