考虑以下集合。
我希望以结构化方式显示它,例如TreeView
。我希望能够围绕整个群体绘制边界等。
如何使用尽可能少的程序代码完成此任务?
答案 0 :(得分:5)
这可以满足您的需求,并且通用:
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
答案 1 :(得分:0)
虽然接受的答案中的代码满足原始问题的需要,但在处理更复杂对象的IEnumerables时它将会失败(因为谓词在将枚举中的最后一项与&进行比较时会抛出异常#34; next&#34; item [根据定义,它将始终为null]。
此版本处理更复杂的对象:
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;
}
答案 2 :(得分:-1)
last = null;
foreach (var option in list)
{
if (last != option)
newlist.Add(new Group(option, new[]));
newlist.Last().Add(option);
last = option;
}
答案 3 :(得分:-1)
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);
}
}
答案 4 :(得分:-1)