在C#中追加行 [英] Appending lines in C#

查看:78
本文介绍了在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 your ToString method - just return the property directly:

public override string ToString()
{
    return this.academic.Name;
}


The File.AppendAllLines method expects the second parameter to be an IEnumerable<string>, but you are passing in a string 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 passing LinkRecords to ToString() method, its taken as LinkRecords.academicName which is wrong because you are not iterating through LinkRecords.

Instead overriding ToString() 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆