使用linq聚合文本文件内容,以便将它们分组

时间:2019-02-26 15:30:24

标签: c# linq aggregate

给出以下查询。

var query = files
            .SelectMany(file => File.ReadAllLines(file))
            .Where(_ => !_.StartsWith("*"))
            .Select(line => new {
                Order = line.Substring(32, 7),
                Delta = line.Substring(40, 3),
                Line = new String[] { line }
            });

这显然会产生具有属性Order: stringDelta: stringLine: string[]的对象列表

我有一个看起来像这样的物品清单。

{ 1, 'A', {'line1'} }, 
{ 1, 'A', {'line2'} }, 
{ 2, 'B', {'line3'} }, 
{ 1, 'B', {'line4 } }

是否有可能使用Linq Aggregate或类似的函数构造来收集所有 adjacent OrderDelta组合在一起,同时累积行。 / p>

因此汇总是包含所有“行”的项目列表

{ 1, 'A', {'line1', 'line2'} }
{ 2, 'B', {'line3'} }
{ 1, 'B', {'line4'} }

由于聚集顺序地进行迭代,因此应该有可能收集所有具有相同字段相等的相邻行。

循环很容易,但是我正在尝试使用一组lambda来实现。

2 个答案:

答案 0 :(得分:1)

注意:不按邻接点将项目分组

您可以将简单的GroupBySelectMany结合使用来产生所需的结果:

var query = new[] {
  new { order = 1, delta = "A", line = new[] { "line1" } },
  new { order = 1, delta = "A", line = new[] { "line2" } },
  new { order = 2, delta = "B", line = new[] { "line3" } },
  new { order = 1, delta = "B", line = new[] { "line4" } },
};

query
  .GroupBy(q => new { q.order, q.delta })
  .Select(q => new {
    order = q.Key.order,
    delta = q.Key.delta,
    lines = q.SelectMany(l => l.line)
});

产生:

resultset

答案 1 :(得分:1)

您需要使用linear1.setOnTouchListener(new View.OnTouchListener() { float initX; float initY; @Override public boolean onTouch(View v, MotionEvent event) { if (v == textview2) { return; } switch(event.getAction()) { case MotionEvent.ACTION_DOWN: initX = event.getX(); initY = event.getY(); break; case MotionEvent.ACTION_UP: initX -= event.getX(); initY -= event.getY(); if (initY > len) { //Logic For Swipe Right } else if(initY < -len) { //Logic For Swipe Left } break; } return true; } }); 的以下变体:

GroupBy

具有该相邻分组,您的代码可以与Chris's answer中的代码相同:

public static class EnumerableExtensions
{
    public class AdjacentGrouping<K, T> : List<T>, IGrouping<K, T>
    {
        public AdjacentGrouping(K key) { Key = key; }
        public K Key { get; private set; }
    }

    public static IEnumerable<IGrouping<K, T>> GroupByAdjacent<T, K>(
                            this IEnumerable<T> sequence, Func<T, K> keySelector)
    {
        using (var it = sequence.GetEnumerator())
        {
            if (!it.MoveNext())
                yield break;
            T curr = it.Current;
            K currKey = keySelector(curr);
            var currentCluster = new AdjacentGrouping<K, T>(currKey) { curr };
            while (it.MoveNext())
            {
                curr = it.Current;
                currKey = keySelector(curr);
                if (!EqualityComparer<K>.Default.Equals(currKey, currentCluster.Key))
                {
                    // start a new cluster
                    yield return currentCluster;
                    currentCluster = new AdjacentGrouping<K, T>(currKey);
                }
                currentCluster.Add(curr);
            };
            // currentCluster is never empty
            yield return currentCluster;
        }
    }
}

免责声明:函数var query = files .SelectMany(file => File.ReadAllLines(file)) .Where(_ => !_.StartsWith("*")) .Select(line => new { Order = line.Substring(32, 7), Delta = line.Substring(40, 3), Line = new String[] { line } }) .GroupByAdjacent(o => new { o.Order, o.Delta }) .Select(g => new { g.Key.Order, g.Key.Delta, Lines = g.Select(o => o.Line).ToList() }); 来自我自己的宠物项目,而不是从任何地方复制的。