【发布时间】: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<int>,所以它不会正确填充在元组内的列表。
更新
列表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 之外我还能使用什么其他数据结构?