我有一个班级Author.cs
:
public class Author
{
public Author()
{ }
public int _AuthorID { get; set; }
public string _AuthorName { get; set; }
public List<Paper> _Papers { get; set; }
}
和另一个班级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; }
}
我希望在Author.cs
类中有一个方法,它返回给定年份的作者和给定年份之间的作者。
我尝试了以下内容:
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();
}
方法GetPaperYear()
尚未定义,但它旨在在类Author.cs
中定义此类方法,以实现此处显示为虚拟的目标。
同样,我希望有类似的方法:
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
}
如何使用Linq实现这一目标?
答案 0 :(得分:0)
使用Linq比您已经使用的更深,并将return语句更改为:
在GetAuthorsByYear()
方法中:
return _fullList.Where(_author => _author._Papers.Any(y => y._Year == _year)).ToList();
在GetAuthorsBetweenYears()
方法中:
return _fullList.Where(_author => _author._Papers.Any(y => y._Year >= _startYear && y._Year <= _endYear))).ToList();
不再需要方法GetPaperYear()
。
答案 1 :(得分:0)
如果你想在问题中提到的Author
类中有单独的方法,它可能看起来像:
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);
}
}
旧方法改变了:
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();
}
还有关于Venue和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;
}
此代码可以重新确定。