在C#中追加行 [英] Appending lines in C#
问题描述
我有一行代码可以为我打开控制台列表,工作正常。
LinkRecords.ForEach(record => Console.WriteLine({ record.academic.Name}));
现在我正在尝试将内容写入文件,我创建了一个ToString对象调用
公共覆盖字符串ToString(){return(${this.academicName});}
和我的代码行是
File.AppendAllLines(@C:\\\\\\\\\\\\\\\\\\ ));
我的错误信息在LinkRecords.ToString()上,说我无法从'string'转换为'System.Collections.Generic.IEnumerable, < string>'
请通过此方式获得任何帮助。
{this.academicName }});}
和我的代码行是
File.AppendAllLines(@C :\pg\Gold\BRdata.txt,L inkRecords.ToString());
我的错误信息在LinkRecords.ToString()上,说我无法从'string'转换为'System.Collections。 Generic.IEnumerable,< string>'
感谢您的任何帮助。
没有必要滥用字符串插值对于ToString
方法 - 只需直接返回属性:
public 覆盖 string ToString()
{
返回 此 .academic.Name;
}
File.AppendAllLines
方法要求第二个参数为IEnumerable< string>
,但您传入的是字符串
。您需要传入一个包含每个项目的字符串表示的列表。 LINQ使这一点变得特别简单:
File.AppendAllLines( @ C:\pg\Gold \BRdata.txt,LinkRecords.Select(r = > r.ToString())) ;
// 或者:
File.AppendAllLines( @ C:\pg\Gold \BRdata.txt,LinkRecords.Select(r = < span class =code-keyword>> r.academic.Name));
当你传递LinkRecords
到ToString()
方法,它被视为LinkRecords.academicName
这是错误的因为你没有遍历LinkRecords
。
改为覆盖ToString()
方法,我会这样做。
private string GetAcademicNames(LinkRecords linkRecords)
{
StringBuilder sb = new StringBuilder();
linkRecords.ForEach(record = > sb.Append(record.academic.Name));
return sb.ToString();
}
File.AppendAllLines( @ C:\\ \\pg\Gold\BRdata.txt跨度>,GetAcademicNames(linkRecords));
I have a line of code that prints a list to console for me which works fine.
LinkRecords.ForEach(record => Console.WriteLine({record.academic.Name}));
Now I am trying to write the contents to a file, I created a ToString object called
public override string ToString(){return($"{this.academicName}");}
and my line of code is
File.AppendAllLines(@"C:\pg\Gold\BRdata.txt",LinkRecords.ToString());
My error message is on the LinkRecords.ToString() saying that I can not convert from 'string' to 'System.Collections.Generic.IEnumerable,<string>'
Appreciate any assistance with this please.
"{this.academicName}");}
and my line of code is
File.AppendAllLines(@"C:\pg\Gold\BRdata.txt",LinkRecords.ToString());
My error message is on the LinkRecords.ToString() saying that I can not convert from 'string' to 'System.Collections.Generic.IEnumerable,<string>'
Appreciate any assistance with this please.
There's no need to abuse string interpolation for yourToString
method - just return the property directly:
public override string ToString() { return this.academic.Name; }
TheFile.AppendAllLines
method expects the second parameter to be anIEnumerable<string>
, but you are passing in astring
instead. You need to pass in a list containing the string representation of each item. LINQ makes this particularly easy:
File.AppendAllLines(@"C:\pg\Gold\BRdata.txt", LinkRecords.Select(r => r.ToString())); // Or: File.AppendAllLines(@"C:\pg\Gold\BRdata.txt", LinkRecords.Select(r => r.academic.Name));
When you are passingLinkRecords
toToString()
method, its taken asLinkRecords.academicName
which is wrong because you are not iterating throughLinkRecords
.
Instead overridingToString()
method, I would do it this way.
private string GetAcademicNames(LinkRecords linkRecords) { StringBuilder sb = new StringBuilder(); linkRecords.ForEach(record => sb.Append(record.academic.Name)); return sb.ToString(); } File.AppendAllLines(@"C:\pg\Gold\BRdata.txt",GetAcademicNames(linkRecords));
这篇关于在C#中追加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!