Ejemplo n.º 1
0
 public static DelayedGroup<T> Create(string groupKey, DateTime createAt)
 {
     var theGroup = new DelayedGroup<T>();
     theGroup.GroupKey = groupKey;
     theGroup.LastItemDate = createAt;
     return theGroup;
 }
Ejemplo n.º 2
0
        public void AppendToGroups(IList<T> items, Func<T, string> getGroupKey, Func<T, DateTime> getAppendAt)
        {
            if (getGroupKey == null)
            {
                throw new ArgumentNullException(nameof(getGroupKey));
            }

            if (getAppendAt == null)
            {
                throw new ArgumentNullException(nameof(getAppendAt));
            }

            if (items == null || items.Count == 0)
            {
                return;
            }

            lock (Lock)
            {
                var groups = items.GroupBy(getGroupKey);
                foreach (var itemGroup in groups)
                {
                    var groupKey = itemGroup.Key;
                    var groupItems = itemGroup.ToList();
                    if (groupItems.Count > 0)
                    {
                        var lastItemDate = groupItems.Max(getAppendAt);
                        DelayedGroups.TryGetValue(groupKey, out var theGroup);
                        if (theGroup == null)
                        {
                            theGroup = DelayedGroup<T>.Create(groupKey, lastItemDate);
                            DelayedGroups.Add(groupKey, theGroup);
                        }
                        theGroup.LastItemDate = lastItemDate;
                        theGroup.AppendItems(groupItems);
                    }
                }
            }
        }