【问题标题】:Fill Tuple item as a List inside a Dictionary将元组项填充为字典中的列表
【发布时间】:2016-07-23 16:14:50
【问题描述】:

我的代码中有一个Dictionary,在一个方法中定义为:

public Dictionary<int, Tuple<List<int>, int, int, int>> GetAuthorData(int startYear, int endYear)  
{  
   Dictionary<int, Tuple<List<int>>, int, int, int>> authorData;  

   foreach (var paper in Papers.Where(p => p.Year >= startYear && p.Year <= endYear))  
   {  
      authorData.Add(paper.PaperID,  
                     new Tuple<List<int>, int, int, int>(  
                     paper.CoAuthors, paper.PaperCategory, paper.VenueID, paper.Year)  
                    );  
   }  
   return authorData;  
}

问题是在添加paper.CoAuthors 时,因为它是List&lt;int&gt;,所以它不会正确填充在元组内的列表。

更新

列表Papers定义为:

public List<Paper> Papers { get; set; }  

Paper.cs 类定义为:

public class Paper
{
   // Class constructor
   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; }
}

GetAuthorData() 用作:

Dictionary<int, Author.AuthorData> tauthorData = eAuthor.GetAuthorData(year, year + 1);
foreach( var kvauthor in tauthorData)
{
   tw.WriteLine("PaperID: {0}, CoAuthors: {1}, PaperCategory: {2}, Venue: {3}, Year: {4}",  
   kvauthor.Key, kvauthor.Value.CoAuthors, kvauthor.Value.PaperCategory,  
   kvauthor.Value.VenueID, kvauthor.Value.Year);
}              

所需输出

PaperID: 1, CoAuthorID: 23, PaperCategory: 6, VenueID: 3454, Year: 2016  
PaperID: 1, CoAuthorID: 24, PaperCategory: 6, VenueID: 3454, Year: 2016  
PaperID: 1, CoAuthorID: 25, PaperCategory: 6, VenueID: 3454, Year: 2016  
PaperID: 1, CoAuthorID: 26, PaperCategory: 6, VenueID: 3454, Year: 2016  
PaperID: 2, CoAuthorID: 27, PaperCategory: 7, VenueID: 3455, Year: 2016  
PaperID: 2, CoAuthorID: 28, PaperCategory: 7, VenueID: 3455, Year: 2016  
PaperID: 2, CoAuthorID: 29, PaperCategory: 7, VenueID: 3455, Year: 2016  

我们如何才能正确添加到CoAuthors 列表?

【问题讨论】:

  • 列表元组的字典?我认为您需要在这里重新考虑您的模型!
  • 我定义了 Dictionary 的结构,ID 是 int,value 是 Tuple
  • @DavidG 你能建议我错在哪里以及如何标准化?
  • 我必须内联堆叠所有这些值,除了 Dictionary 之外我还能使用什么其他数据结构?

标签: c# list tuples


【解决方案1】:

你需要初始化字典:

public Dictionary<int, AuthorData> GetAuthorData(int startYear, int endYear)
{
    var authorData = new Dictionary<int, AuthorData>();

    foreach (var paper in Papers.Where(p => p.Year >= startYear && p.Year <= endYear))
    {
        Console.WriteLine(paper.CoAuthors.Count.ToString());
        authorData.Add(paper.PaperID,
                        new AuthorData()
                        {
                            CoAuthors = paper.CoAuthors,
                            PaperCategory = paper.PaperCategory,
                            VenueID = paper.VenueID,
                            Year = paper.Year
                        });
        Console.WriteLine("After");
        Console.WriteLine(authorData.Last().Value.Count.ToString());
    }

    return authorData;
}

class AuthorData
{
       public List<int> CoAuthors { set; get; }
       public int PaperCategory { set; get; }
       public int VenueID { set; get; }
       public int Year { set; get; }
}

至于您的打印代码,您需要这样做:

Dictionary<int, Author.AuthorData> tauthorData = eAuthor.GetAuthorData(year, year + 1);
foreach (var kvauthor in tauthorData)
{
    foreach (var author in kvauthor.Value.CoAuthors)
    {
        tw.WriteLine("PaperID: {0}, CoAuthors: {1}, PaperCategory: {2}, Venue: {3}, Year: {4}",
        kvauthor.Key, author, kvauthor.Value.PaperCategory,
        kvauthor.Value.VenueID, kvauthor.Value.Year);
    }
}

【讨论】:

  • 我个人会一直使用 LINQ 语法 (ToDictionary)
  • 问题是我已经展示了我使用这种方法的方式,即GetAuthorData(),并且没有打印CoAuthors中的值
  • @Taufel 请向您的问题添加示例输出,如下所示:"PaperID: 100, CoAuthors: ???? , PaperCategory: 10, Venue: 5, Year: 2016"
  • 是的,您自己通过评论添加了示例,而???? 将是CoAuthors 的ID,如果PaperID = 100 在列表中有多个CoAuthors,那么这个PaperID 和所有只要我们在CoAuthors 列表中有元素,其他项目就应该重复
  • @Taufel 然后将kvauthor.Value.CoAuthors 替换为String.Join(", ", kvauthor.Value.CoAuthors)
猜你喜欢
  • 2017-09-04
  • 2017-04-07
  • 1970-01-01
  • 2013-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-02
  • 2016-11-25
相关资源
最近更新 更多