使用LINQ自定义类返回列表 [英] Return list of custom class using Linq

查看:141
本文介绍了使用LINQ自定义类返回列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类 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; }
}  



我想在作者的方法。 CS 类,它给出岁之间返回给定年份的作者和作者。

I want to have a method in Author.cs class which returns authors for given Year and author between given Years.

我试过如下:

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 这样的方法来实现,这就是为什么在这里显示为假目标。

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
}

如何实现这一目标使用LINQ?

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);
    }
}

和老办法改变:

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;
    }       

这代码可以重构肯定。

这篇关于使用LINQ自定义类返回列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆