我必须存储此结果集(示例):
Author_ID | Author_Name | CoAuthor_ID | Paper_ID | Venue_ID | Year
------------------------------------------------------------------
677 | Nuno Vascon | 901706 | 812229 | 64309 | 2005
677 | Nuno Vascon | 901706 | 812486 | 65182 | 2005
677 | Nuno Vascon | 901706 | 818273 | 185787 | 2005
1359 | Peng Shi | 133112 | 812558 | 65182 | 2005
1359 | Peng Shi | 266386 | 812558 | 65182 | 2005
1359 | Peng Shi | 454557 | 812558 | 65182 | 2005
... | ... | ... | ... | ... | ...
在课堂上的对象。即对于相同的Year = 2005
,我必须存储不同的Author_ID
,Author_Name
,CoAuthor_ID
,Paper_ID
和Venue_ID
,这些可以更大数字,而这里只是一个例子。
编辑:
SELECT AC.Author_ID, A.Author_Name, AC.CoAuthor_ID, AC.Paper_ID, AP.Venue_ID, AC.Year
FROM AuthorCoAuthor AC
JOIN AuthorPaper AP ON AP.Author_ID = AC.Author_ID AND
AP.Paper_ID = AC.Paper_ID
JOIN Author A ON A.Author_ID = AC.Author_ID
WHERE AC.Year = 2005
ORDER BY AC.Author_ID, AC.Year, AC.CoAuthor_ID, AC.Paper_ID, AP.Venue_ID
目的
为了适应这种方法,我问了这个问题。方法如下:
public List<Author> CollectAuthors(int _year)
{
List<Author> _eAthors = new List<Author>();
// Get authors from database including all the information in rows for `Author_ID`
...
return _eAuthors;
}
如何将此数据存储在类的对象中?
答案 0 :(得分:0)
您可以创建两个单独的类,例如Author
:
public class Author
{
public int Author_ID { get; set; }
public string Author_Name { get; set; }
public int Year { get; set; }
public List<AuthorData> Data { get; set; }
}
和类AuthorData
一样:
public class AuthorData
{
public int CoAuthor_ID { get; set; }
public int Paper_ID { get; set; }
public int Venue_ID { get; set; }
}
然后你可以创建这样的实例:
List<Author> authors = new List<Author>();
// assuming you have a SqlCommand "command" executing the query you showed
using (SqlDataReader reader = command.ExecuteReader())
{
while(reader.Read())
{
int author_ID = reader.GetInt(0);
Author author = authors.FirstOrDefault(a => a.Author_ID == author_ID);
if (author == null)
{
author = new Author{
Author_ID = author_ID,
Author_Name = reader.GetString(1),
Year = reader.GetInt(5),
Data = new List<AuthorData>()
};
authors.Add(author);
}
author.Data.Add(new AuthorData{
CoAuthor_ID = reader.GetInt(2),
Paper_ID = reader.GetInt(3),
Venue_ID = reader.GetInt(4)
});
}
}
这假设您的ID是Int32
而不是Int64
,当然您应该添加错误处理。
所以,让我们说你Author
中有List<Author>
个Authors
。然后,您可以选择特定年份的所有public List<Author> AuthorsOfYear(List<Author> fullList, int year)
{
return fullList.Where(author => author.Year == year).ToList();
}
,如下所示:
public List<Author> AuthorsOfYear(List<Author> fullList, int startyear, int endyear)
{
return fullList.Where(author =>
author.Year >= startyear && author.Year <= endyear).ToList();
}
和一年的范围:
exception:bind failed: EACCES (Permission denied)