如何收集的组连续的类似项目吗?(How to group consecutive similar i

2019-07-19 15:36发布

请看下面的集合。

  • 真正
  • 真正
  • 真正

我想在一个结构化的方式显示出来,说,在TreeView 。 我希望能够吸引周围的整个群体和这样的边界。

  • 正式组
    • 真正
  • 假组
  • 正式组
    • 真正
    • 真正
  • 假组

如何用尽可能少的程序代码尽可能地做到这一点?

Answer 1:

这样做,你要寻找的,是通用的:

private static IEnumerable<IGrouping<int, T>> GroupConsecutive<T>(this IEnumerable<T> set, Func<T, T, bool> predicate)
{
    var i = 0;
    var k = 0;
    var ranges = from e in set
                 let idx = ++i
                 let next = set.ElementAtOrDefault(idx)
                 let key = (predicate(e, next)) ? k : k++
                 group e by key into g
                 select g;
    return ranges;
}

用法:

var set = new List<bool>
            {
                true,
                false,
                false,
                false,
                true,
                true,
                false,
                false,
            };
var groups = set.GroupConsecutive((b1, b2) => (b1 == b2));
foreach (var g in groups)
{
    Console.WriteLine(g.Key);
    foreach (var b in g)
        Console.WriteLine("\t{0}", b);
}

输出:

0
        True
1
        False
        False
        False
2
        True
        True
3
        False
        False


Answer 2:

而在接受的答案代码满足原来的问题的需要,它会处理更复杂的对象的IEnumerables时(因为谓语往往会比较在枚举的最后一个项目,“下一个”项目时抛出一个异常翻倒[其中,根据定义,将始终是空])。

此版本处理更复杂的对象:

   public static IEnumerable<IGrouping<int, T>> GroupConsecutive<T>(this IEnumerable<T> set, Func<T, T, bool> predicate)
    {
        var i = 0;
        var k = 0;
        var ranges = from e in set
                     let idx = ++i
                     let next = set.ElementAtOrDefault(idx)
                     let key = next == null ? k : (predicate(e, next)) ? k : k++
                     group e by key into g
                     select g;
        return ranges;
    } 


Answer 3:

last = null;
foreach (var option in list)
{
   if (last != option)
      newlist.Add(new Group(option, new[]));
   newlist.Last().Add(option);
   last = option;
}


Answer 4:

public class GroupConsecutiveEqualItemsConverter : IValueConverter
{
    static readonly object UnsetValue = new object();

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        IEnumerable source = value as IEnumerable;
        if (source == null) return DependencyProperty.UnsetValue;
        string propertyName = parameter as string;
        var result = new ObservableCollection<List<object>>();

        var notify = value as INotifyCollectionChanged;
        if (notify != null) notify.CollectionChanged += delegate { Reload(result, source, propertyName); };

        Reload(result, source, propertyName);
        return result;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }

    void Reload(ObservableCollection<List<object>> result, IEnumerable source, string propertyName)
    {
        result.Clear();
        object previous = UnsetValue;
        List<object> group = null;
        foreach (object i in source)
        {
            object current = UnsetValue;
            if (propertyName == null)
            {
                current = i;
            }
            else
            {
                try
                {
                    var property = i.GetType().GetProperty(propertyName);
                    if (property != null) current = property.GetValue(i, null);
                }
                catch (AmbiguousMatchException) { }
            }
            if (!object.Equals(previous, current))
            {
                if (group != null) result.Add(group);
                group = new List<object>();
            }
            group.Add(i);
            previous = current;
        }
        if (group != null && group.Count > 0) result.Add(group);
    }
}


Answer 5:

你可能想看看假的TreeGrid上延迟的博客在http://blogs.msdn.com/delay/archive/2009/09/23/if-it-walks-like-a-duck-and-talks-like-一个-鸭-它,必须待A-的TreeGrid-A-简单XAML只-的TreeGrid的UI换wpf.aspx


(来源: msdn.com )



文章来源: How to group consecutive similar items of a collection?