Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何根据字符串的出现情况将文件内容分组到块中_C#_.net_Linq - Fatal编程技术网

C# 如何根据字符串的出现情况将文件内容分组到块中

C# 如何根据字符串的出现情况将文件内容分组到块中,c#,.net,linq,C#,.net,Linq,所讨论的文件可以有一个或多个块,每个块都以Processname:开头;ABC采购 使用Linq根据行“Processname:;ABC Buying”的出现情况将文件内容拆分为块的最佳方法是什么 这似乎不能正常工作 var lines = File.ReadAllLines(path).OfType<string>().ToList(); var grps = lines.GroupBy(blocks => blocks.Contains("Processname:;ABC

所讨论的文件可以有一个或多个块,每个块都以Processname:开头;ABC采购

使用Linq根据行“Processname:;ABC Buying”的出现情况将文件内容拆分为块的最佳方法是什么

这似乎不能正常工作

var lines = File.ReadAllLines(path).OfType<string>().ToList();
var grps = lines.GroupBy(blocks => blocks.Contains("Processname:;ABC Buying"));
简单易用:

var lines = File.ReadLines(path);
List<List<string>> groups = new List<List<string>>();
List<string> current = null;
foreach(var line in lines){
    if (line.Contains("Processname:;ABC Buying")){
        current = new List<string>();
        groups.Add(current);
    }
    else if (current != null) {
        current.Add(line);
    }
}
var lines=File.ReadLines(路径);
列表组=新列表();
列表当前=空;
foreach(行中的var行){
if(line.Contains(“Processname:;ABC购买”)){
当前=新列表();
组。添加(当前);
}
else if(当前!=null){
当前。添加(行);
}
}

所以。。你真的应该像艾哈迈德展示的那样

尽管如此,您可以仅使用Linq来执行类似于以下(效率不高)代码的操作:

var行=新[]{“wierd”、“a1”、“b1”、“b2”、“b3”、“a2”、“b4”、“a3”、“b5”、“b6”};
列表组=行
.Select((x,i)=>Tuple.Create(x,x.StartsWith(“a”)?new int?(i):null))
.Aggregate(Tuple.Create(Enumerable.Empty(),null),
(acc,x)=>x.Item2.HasValue
?Tuple.Create(acc.Item1.Concat(new[]{Tuple.Create(x.Item1,x.Item2???-1)}),x.Item2)
:Tuple.Create(acc.Item1.Concat(new[]{Tuple.Create(x.Item1,acc.Item2???-1)),acc.Item2))
.项目1
.GroupBy(x=>x.Item2)
.Select(x=>x.Select(y=>y.Item1.ToList())
.ToList();
foreach(组中的var组)
{
Console.WriteLine(“--newgroup--”);
foreach(组中的var行)
{
控制台写入线(行);
}
}
在这里测试它:

var lines = File.ReadLines(path);
List<List<string>> groups = new List<List<string>>();
List<string> current = null;
foreach(var line in lines){
    if (line.Contains("Processname:;ABC Buying")){
        current = new List<string>();
        groups.Add(current);
    }
    else if (current != null) {
        current.Add(line);
    }
}
var lines = new[] { "wierd", "a1", "b1", "b2", "b3", "a2", "b4", "a3", "b5", "b6" };
List<List<string>> groups = lines
    .Select((x, i) => Tuple.Create(x, x.StartsWith("a") ? new int?(i) : null))
    .Aggregate(Tuple.Create<IEnumerable<Tuple<string, int>>, Nullable<int>>(Enumerable.Empty<Tuple<string, int>>(), null), 
        (acc, x) => x.Item2.HasValue
            ? Tuple.Create(acc.Item1.Concat(new[] { Tuple.Create(x.Item1, x.Item2 ?? -1) }), x.Item2)
            : Tuple.Create(acc.Item1.Concat(new[] { Tuple.Create(x.Item1, acc.Item2 ?? -1) }), acc.Item2))
    .Item1
    .GroupBy(x => x.Item2)
    .Select(x => x.Select(y => y.Item1).ToList())
    .ToList();

foreach(var group in groups) 
{
    Console.WriteLine("--New group--");
    foreach (var line in group)
    {
        Console.WriteLine(line);
    }
}