I have a class Author.cs
as:
public class Author
{
public Author()
{ }
public int _AuthorID { get; set; }
public string _AuthorName { get; set; }
public List<Paper> _Papers { get; set; }
}
and another class Paper.cs
as:
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; }
}
I want to have a method in Author.cs
class which returns authors for given Year and author between given Years.
I tried as following:
public List<Author> GetAuthorsByYear(List<Author> _fullList, int _year)
{
return _fullList.Where(_author => _author.GetPaperYear() == _year).ToList();
}
public List<Author> GetAuthorsBetweenYears(List<Author> _fullList, int _startYear, int _endYear)
{
return _fullList.Where(_author => _author.GetPaperYear() >= _startYear && _author.GetPaperYear() <= _endYear).ToList();
}
The method GetPaperYear()
is not defined yet but it is intended to define such a method in class Author.cs
to achieve the goal that's why shown here as dummy.
Similarly, I want to have methods like:
public List<int> GetCoAuthorsByYear(int _year)
{
// How to return list of coauthors by year
}
public List<int> GetCoAuthorsBetweenYears(int _startYear, int _endYear)
{
// How to return list of coauthors between years
}
public List<int> GetVenuesByYear(int _year)
{
// How to return list of venues by year
}
public List<int> GetVenuesBetweenYears(int _startYear, int _endYear)
{
// How to return list of venues between years
}
How to achieve this using Linq?
If you want to have separate method in the Author
class like you asked in the question, it could look like:
public class Author
{
public Author()
{ }
public int _AuthorID { get; set; }
public string _AuthorName { get; set; }
public List<Paper> _Papers { get; set; }
public bool HasPapersForYear(int year)
{
return _Papers.Any(_paper => _paper._Year == year);
}
public bool HasPapersForYears(int startYear, int endYear)
{
return _Papers.Any(_paper => _paper._Year >= startYear && _paper._Year <= endYear);
}
}
And old methods are changed:
public List<Author> GetAuthorsByYear(List<Author> _fullList, int _year)
{
return _fullList.Where(_author => _author.HasPapersForYear(_year)).ToList();
}
public List<Author> GetAuthorsBetweenYears(List<Author> _fullList, int _startYear, int _endYear)
{
return _fullList.Where(_author => _author.HasPapersForYears(_startYear,_endYear)).ToList();
}
Also about the part of Venue and CoAuthors
public List<int> GetCoAuthorsByYear(int _year)
{
var coAuthors = new List<int>();
foreach(var paper in _Papers.Where(_paper => _paper._Year == _year))
{
coAuthors.AddRange(paper._CoAuthors);
}
return coAuthors;
}
public List<int> GetCoAuthorsBetweenYears(int _startYear, int _endYear)
{
var coAuthors = new List<int>();
foreach(var paper in _Papers.Where(_paper => _paper._Year >= _startYear && _paper._Year <= _endYear))
{
coAuthors.AddRange(paper._CoAuthors);
}
return coAuthors;
}
public List<int> GetVenuesByYear(int _year)
{
var venues = new List<int>();
foreach(var paper in _Papers.Where(_paper => _paper._Year == _year))
{
venues.Add(paper._VenueID);
}
return venues;
}
public List<int> GetVenuesBetweenYears(int _startYear, int _endYear)
{
var venues = new List<int>();
foreach(var paper in _Papers.Where(_paper => _paper._Year >= _startYear && _paper._Year <= _endYear))
{
venues.Add(paper._VenueID);
}
return venues;
}
This code could be refactored sure.