如何使用linq在列表中插入文本



我对c#中的linq编程很陌生,我有下面的文本文件,读取文件并存储在列出

IP/文本文件DC列表:

North: &North
**arr prod_test**
Dcname BN14
south: &south
**arr prod_test**
Dcname BN1
East: &East
**arr ff_test**
DcName RD1
NorthEast: &NorthEast
**arr mc_test**
DcName RR
WestEast: &NorthEast
**arr mc_test**
DcName RR

O/p文件:

DC列表:

North: &North
**arr prod_test**
Dcname BN14
south: &south
**arr prod_test**
Dcname BN1
East: &East
**arr ff_test**
DcName RD1
/// here I need to add the new text my doubt is how to find the  insert position and how to add the content using linq query 
West: &West
**arr ff_test**
DcName RD1
NorthEast: &NorthEast
**arr mc_test**
DcName RR
WestEast: &NorthEast
**arr mc_test**
DcName RR

我需要搜索arr_ff_test,并需要在East之前插入下一个内容:&东请帮我用linq。

List<string> lines = File.ReadAllLines(buildOutFile).ToList();
index = lines.FindIndex(element => element.Contains("arr ff_test"));
temp = String.Format($"West: &Westn    **arr ff_test**nDcName RD1);
lines.Insert(index-1, temp);

我想把上面三行改成linq

假设条目的位置以某种方式计数,我理解您需要在东部之后插入引用西部的块。也就是说在东北一号之前,对吗?在这种情况下,查找NorthEast并插入前面似乎比查找East并在后面插入更容易。

在这种情况下,代码变为:

private List<string> Merge2(string buildOutFile)
{
var lines = File.ReadAllLines(buildOutFile).ToArray();
var textToInsert = new List<string>()
{
"West: &West",
"**arr ff_test**",
"DcName RD1"
}.ToArray();
var oneLineSource = string.Join(Environment.NewLine, lines);
var oneLineinsertion = string.Join(Environment.NewLine, textToInsert);
var separator = "NorthEast: &NorthEast";
var twoParts = oneLineSource.Split(separator);
string onelinerOutput = twoParts[0] + oneLineinsertion + twoParts[1];
return onelinerOutput.Split().ToList();
}

如果你想不惜一切代价使用linq:

private List<string> Merge(string buildOutFile)
{
var lines = File.ReadAllLines(buildOutFile);
var separator = "NorthEast: &NorthEast";
var textToInsert = $"{separator}{Environment.NewLine}West: &West{Environment.NewLine}**arr ff_test**{Environment.NewLine}DcName RD1{ Environment.NewLine}"; 
return lines.Select(line=>line.Contains(separator)?textToInsert:line).ToList();                    
}

如果您的附加文本不是硬编码的,您可以简单地在分隔符插入的字符串中放置其他占位符,以使其可自定义。

正如我在评论中提到的,您需要在这里使用流。您不应该读取数组中的行,然后使用LINQ来执行您想要的操作,因为这种迭代集合并修改它的操作是糟糕的设计。

这是我测试的一个示例代码。我添加了注释来解释我的代码。

public void ReadText()
{
// Stream for the existing file "sample.txt"
using (StreamReader input = new StreamReader(new FileStream("sample.txt", FileMode.Open)))
{
// Stream for output file that is to be created or overwritten
using (StreamWriter output = new StreamWriter(new FileStream("output.txt", FileMode.OpenOrCreate)))
{
// We need to look ahead two lines at a time
string second_line, first_line;
first_line = input.ReadLine();
// This loop goes on for every line in the input file
while ((second_line = input.ReadLine()) != null)
{
if (second_line.Contains("arr ff_test")) // This is the line where the insertions are to be made
{
// Now insert the extra lines you want in your output file
output.WriteLine("West: &West");
output.WriteLine("**arr ff_test**");
output.WriteLine("DcName RD1");
output.WriteLine(); // Make an empty line
}
output.WriteLine(first_line); // write the same line in output as it is
first_line = second_line; // the first line is moved foreward
}
output.Write(first_line); // The last line is yet to be written
// Close both the files
output.Close();
}
input.Close();
}
}

这里的诀窍是一次向前看两行。我用你给定的输入和输出文件测试了它,看起来工作得很好。

假设输入文件由3行的块组成,每个块后面跟着一行空行,则只需要该"arr ff_test"条目的行索引,然后计算下一个4的倍数。插入您的附加行。你完了
'LinQ'中的'Q'表示查询列表的功能最强。不建议将其用于插入文本。List<string>是一个内置了所有必要功能的工具。

var lines = File.ReadAllLines(inputFilePath).ToList();
var matchPos = lines.IndexOf("**arr ff_test**");
if (matchPos > -1)  {
// get next multiple of 4 as insertion index ...
var insertPos = ((matchPos / 4) + 1) * 4; 
lines.InsertRange(insertPos, new[] {
"West: &West", "**arr ff_test**", "DcName RD1", ""});
File.WriteAllLines(outputFilePath, lines.ToArray());
}

最新更新