C# 使用TextWriter编写自定义列表
我有一个类C# 使用TextWriter编写自定义列表,c#,list,textwriter,C#,List,Textwriter,我有一个类Author.cs为: public class Author { public Author() { } public int AuthorID; public string AuthorName; public List<Papers> Papers; // ... rest of the methods... } public class Paper { public Pape
Author.cs
为:
public class Author
{
public Author()
{ }
public int AuthorID;
public string AuthorName;
public List<Papers> Papers;
// ... rest of the methods...
}
public class Paper
{
public Paper()
{ }
public int PaperID { get; set; }
public List<int> CoAuthors { get; set; }
public int VenueID { get; set; }
public int PaperCategory { get; set; }
public int Year { get; set; }
}
我尝试在文本文件中打印为:
TextWriter twObjClus = File.CreateText(Path.Combine(path, objClusterFilename));
foreach (CurrentCluster curCluster in curClusters)
{
twObjClus.WriteLine(@"ClusterID: {0}, ClusterSize: {1} \n",
curCluster.GetClusterID(), curCluster.GetClusterSize()
);
twObjClus.WriteLine("--------------------------------------------------------------");
foreach (EvoObject eEvoObject in curCluster.GetClusterObjects())
{
Author eAuthor = (Author)eEvoObject.GetOriginalObject(); // it gives Object ID
twObjClus.WriteLine(@"AuthorID: {0}, AuthorName: {1}, PaperID: {2}, Year: {3} \n",
eAuthor.AuthorID, eAuthor.AuthorName,
eAuthor.Papers.Select(p => p.PaperID),
eAuthor.Papers.Select(y => y.Year)
);
}
twObjClus.WriteLine("\n\n");
}
twObjClus.Flush();
twObjClus.Close();
我正确地在文本文件中打印了作者ID
和作者姓名
,但没有打印纸张ID
和年份
,而在文本文件中打印的语句不是这两个值:
System.Linq.Enumerable+WhereSelectListIterator`2[DirichletProcessClustering.GraphData.Paper,System.Int32]
如何在文本文件中打印所有这些值?您需要计算以下项返回的IEnumerable:
eAuthor.Papers.Select(p => p.PaperID)
字符串格式化程序将在IEnumerable
上调用ToString
,这就是为什么您会得到:
System.Linq.Enumerable+WhereSelectListIterator`2[DirichletProcessClustering.GraphData.Paper,System.Int32]
您可以添加一个额外的foreach
语句,并列举上面的IEnumerable
来为作者编写论文ID和年份,如下所示:
foreach (EvoObject eEvoObject in curCluster.GetClusterObjects())
{
Author eAuthor = (Author)eEvoObject.GetOriginalObject();
twObjClus.WriteLine(
@"AuthorID: {0}, AuthorName: {1} \n",
eAuthor.AuthorID,
eAuthor.AuthorName);
foreach (var paper in eAuthor.Papers)
{
twObjClus.WriteLine(
@"PaperID: {0}, Year: {1}", paper.PaperID, paper.Year);
}
}
您需要计算以下项返回的IEnumerable:
eAuthor.Papers.Select(p => p.PaperID)
字符串格式化程序将在IEnumerable
上调用ToString
,这就是为什么您会得到:
System.Linq.Enumerable+WhereSelectListIterator`2[DirichletProcessClustering.GraphData.Paper,System.Int32]
您可以添加一个额外的foreach
语句,并列举上面的IEnumerable
来为作者编写论文ID和年份,如下所示:
foreach (EvoObject eEvoObject in curCluster.GetClusterObjects())
{
Author eAuthor = (Author)eEvoObject.GetOriginalObject();
twObjClus.WriteLine(
@"AuthorID: {0}, AuthorName: {1} \n",
eAuthor.AuthorID,
eAuthor.AuthorName);
foreach (var paper in eAuthor.Papers)
{
twObjClus.WriteLine(
@"PaperID: {0}, Year: {1}", paper.PaperID, paper.Year);
}
}
我必须把这个
foreach()?那么是的。是否有必要使用string.Format()
因为我以前没有使用它是的,你是对的,你不需要string.Format()。我更新了上面的代码来告诉你放foreach的位置谢谢,我想它现在应该可以工作了,因为我的应用程序需要一些时间来完成执行,在那之后,我将能够知道它是否工作了,我必须把这个foreach()
循环放在我的代码中的什么地方?你正在写同一个文本文件,不是吗?那么是的。是否有必要使用string.Format()
因为我以前没有使用它是的,你是对的,你不需要string.Format()。我更新了上面的代码,告诉你在哪里放置foreach谢谢,我想它现在应该可以工作了,因为我的应用程序需要一些时间来完成执行,之后就可以知道它是否工作了