2012-07-21 64 views
4

我为此找到了这个问题,而且我确信我只是错过了它,因为我对Linq没那么好。通过键/属性将列表按键/属性分组到列表中(不会改变列表顺序)(只需通过attrib将列表切入列表中)

我看起来像一个列表:

type=a, value=aaaa 
type=a, value=bbbb 
type=b, value=cccc 
type=d, value=dddd 
type=d, value=eeee 
type=d, value=ffff 
type=a, value=gggg 
type=b, value=hhhh 
type=b, value=iiii 
type=b, value=jjjj 

我想打破这种拆分成子列表,不排序(我需要原来的顺序保持)。我想回到这些列表中,按列表或类似的顺序列出:

List 1 
type=a, value=aaaa 
type=a, value=bbbb 

List2 
type=b, value=cccc 

List 3 
type=d, value=dddd 
type=d, value=eeee 
type=d, value=ffff 

List 4 
type=a, value=gggg 

List 5 
type=b, value=hhhh 
type=b, value=iiii 
type=b, value=jjjj 

我会想象循环不是最好的答案。

任何想法非常赞赏。

很长时间stackoverflow.com!

克里斯

编辑后四个答案:

我查了答案来自: * Enigmativity *伯特·埃文斯 *风险马丁 * david.s

他们所有的工作很好。伯特埃文斯提出了表现,在这种情况下这不是我最关心的问题,但我为了这个帖子做了一些快速检查。

我没有修改任何人的代码,只是在相当短的列表中做了4,000个这样的操作。

风险的回答是最快的。伯特的答案只是慢了一点。

大卫的和Enigmativity几乎没有任何慢,真的。

我标记了Risky的回答,因为表现良好,并且早期指向相关文章,然后回来提供答案。

我会同意伯特的是最可读的,但。

我真的不知道我会用哪一个...实际上,我会使用Enigmativity的解决方案,因为它已经考虑到我只需要每个子组的值和一个键。

+1

[Here's](http://stackoverflow.com/q/11512898/480799)的相关讨论。 – 2012-07-21 22:08:47

回答

3

这可能是全身性或做成一个扩展方法,但你的想法:

public static IEnumerable<List<Item>> GroupConsecutive(IEnumerable<Item> items) 
{ 
    if (items.Any()) 
    { 
     string firstType = items.Select(i => i.Type).First(); 
     var adjacents = items.TakeWhile(i => i.Type == firstType).ToList(); 
     yield return adjacents; 
     foreach (var group in GroupConsecutive(items.Skip(adjacents.Count))) 
     { 
      yield return group; 
     } 
    } 
} 

使用这个类:

public class Item 
{ 
    public string Type { get; set; } 
    public string Value { get; set; } 
} 

编辑:下面是该解决方案的权衡:

优点:

  • 返回一个懒洋洋地评估收集
  • 不发生变异的变量
  • 简洁

缺点:

  • 遍历items两次。如果items是List,这不是什么大问题,但是如果items是一个IEnumerable,它对每个项目执行昂贵的计算,则此方法可能比其他方法更慢。

如果你想items一次懒评估进行迭代,我建议在this答复中提到或yield return循环的GroupAdjacent扩展方法。如果你想要一次迭代没有懒惰的评估,我建议循环或Aggregate方法。

+0

@chrismead我列出了此解决方案的权衡以及基于人们想要的替代解决方案的建议。我希望这有助于您的决定! – 2012-07-23 14:38:10

4

使用此answer稍微修改扩展方法,这answer

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; 
} 

,并给予类:

public class Foo 
{ 
    public string Type { get; set; } 
    public string Value { get; set; } 
} 

你可以这样做:

List<Foo> list = new List<Foo>() 
{ 
    new Foo() { Type = "a", Value = "aaaa" }, 
    new Foo() { Type = "a", Value = "bbbb" }, 
    new Foo() { Type = "b", Value = "cccc" }, 
    new Foo() { Type = "d", Value = "dddd" }, 
    new Foo() { Type = "d", Value = "eeee" }, 
    new Foo() { Type = "d", Value = "ffff" }, 
    new Foo() { Type = "a", Value = "gggg" }, 
    new Foo() { Type = "b", Value = "hhhh" }, 
    new Foo() { Type = "b", Value = "iiii" }, 
    new Foo() { Type = "b", Value = "jjjj" } 
}; 

var groups = list.GroupConsecutive((a, b) => a.Type == b.Type); 

foreach (var group in groups) 
{ 
    Console.WriteLine("List " + group.Key); 
    foreach (var item in group) 
    { 
     Console.WriteLine("Type=" + item.Type + " Value=" + item.Value); 
    } 
    Console.WriteLine(); 
} 

而结果如下所示:

List 0 
Type=a Value=aaaa 
Type=a Value=bbbb 

List 1 
Type=b Value=cccc 

List 2 
Type=d Value=dddd 
Type=d Value=eeee 
Type=d Value=ffff 

List 3 
Type=a Value=gggg 

List 4 
Type=b Value=hhhh 
Type=b Value=iiii 
Type=b Value=jjjj 
2

这是LINQ唯一答案,使用Aggregate

我开始用这样的:

var list = new [] 
{ 
    new { type="a", value="aaaa", }, 
    new { type="a", value="bbbb", }, 
    new { type="b", value="cccc", }, 
    new { type="d", value="dddd", }, 
    new { type="d", value="eeee", }, 
    new { type="d", value="ffff", }, 
    new { type="a", value="gggg", }, 
    new { type="b", value="hhhh", }, 
    new { type="b", value="iiii", }, 
    new { type="b", value="jjjj", }, 
}; 

然后这样做:

var accumulator = new List<KeyValuePair<string, List<string>>>() 
{ 
    new KeyValuePair<string, List<string>>(
     list.First().type, 
     new List<string>()), 
}; 

var results = list.Aggregate(accumulator, (a, x) => 
{ 
    if (a.Last().Key == x.type) 
    { 
     a[a.Count - 1].Value.Add(x.value); 
    } 
    else 
    { 
     a.Add(new KeyValuePair<string, List<string>>(
      x.type, 
      new List<string>(new [] { x.value, }))); 
    } 
    return a; 
}); 

results则是这样的:

results

让我知道如果这个作品为你。

2

没有LINQ,普通的旧c#和每个初级开发人员都希望遵循的单一循环。更不用说更快。

public class Foo 
{ 
    public string Type { get; set; } 
    public string Value { get; set; } 
} 

List<Foo> list = new List<Foo>() 
{ 
    new Foo() { Type = "a", Value = "aaaa" }, 
    new Foo() { Type = "a", Value = "bbbb" }, 
    new Foo() { Type = "b", Value = "cccc" }, 
    new Foo() { Type = "d", Value = "dddd" }, 
    new Foo() { Type = "d", Value = "eeee" }, 
    new Foo() { Type = "d", Value = "ffff" }, 
    new Foo() { Type = "a", Value = "gggg" }, 
    new Foo() { Type = "b", Value = "hhhh" }, 
    new Foo() { Type = "b", Value = "iiii" }, 
    new Foo() { Type = "b", Value = "jjjj" } 
}; 

Foo previous = null; 
List<List<Foo>> separateLists = new List<List<Foo>>(); 
List<Foo> currentList = null; 
foreach (var foo in list) 
{ 
    if (null == previous || previous.Type != foo.Type) 
    { 
     currentList = new List<Foo>(); 
     separateLists.Add(currentList); 
    } 
    currentList.Add(foo); 
    previous = foo; 
}