使用TextWriter编写自定义列表



我有一个 Author.cs as:

public class Author  
{  
   public Author()  
   { }  
   public int AuthorID;  
   public string AuthorName;  
   public List<Papers> Papers;  
   // ... rest of the methods...  
}  

和另一个类Paper.cs为:

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("nn");
}
twObjClus.Flush();
twObjClus.Close();  

我在文本文件中得到正确的打印Author_IDAuthor_Name,但没有打印Paper_IDYear,而这两个值都在文本文件中打印的语句是:

system.linq.shumerable whereselectListiterator`2 [dirichletProcessClustering.graphdata.paper,system.int.int32]

如何在文本文件中打印所有这些值?

您需要评估以下返回的IEnumerable:

eAuthor.Papers.Select(p => p.PaperID)

字符串格式化将在IEnumerable上调用ToString,这就是为什么您得到:

system.linq.shumerable whereselectListiterator`2 [dirichletProcessClustering.graphdata.paper,system.int.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);
       }
}

相关内容

  • 没有找到相关文章

最新更新