Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取自定义类的对象中的值_C#_Sql Server - Fatal编程技术网

C# 获取自定义类的对象中的值

C# 获取自定义类的对象中的值,c#,sql-server,C#,Sql Server,我必须存储此结果集(示例): 在类的对象中。i、 e.对于同一个年份=2005,我必须存储不同的作者ID,作者姓名,合著者ID,论文ID和场馆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.Autho

我必须存储此结果集(示例):

在类的对象中。i、 e.对于同一个
年份=2005
,我必须存储不同的
作者ID
作者姓名
合著者ID
论文ID
场馆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
目的

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;  
}
公开作者列表(国际年)
{
列表_eAthors=新列表();
//从数据库中获取作者,包括“Author\u ID”行中的所有信息
...  
返回_eAuthors;
}

如何将此数据存储在类的对象中?

您可以像下面这样创建两个独立的类
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; }
}
然后您可以创建这样的实例:

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)
        });
    }
}
在一年范围内:

public List<Author> AuthorsOfYear(List<Author> fullList, int startyear, int endyear)
{
    return fullList.Where(author => 
          author.Year >= startyear && author.Year <= endyear).ToList();
}
public List authors of year(List fullList,int startyear,int endyear)
{
返回完整列表。其中(作者=>

author.Year>=startyear&&author.Year可能通过使用?创建数据库,然后让实体框架数据库首先为您生成类如果他使用ADO会怎么样?@LuisLavieri EF生成大多数代码不可用的类表示不需要。合著者引用author吗?我的意思是它本身是FK吗?@Rene--我可以在C#edito中使用相同的查询吗r在
CollectAuthors
从数据库或其他地方读取数据的方法中,我可能需要修改它?@maliks编辑了我的答案,以演示如何通过
SqlDataReader
@maliks获取作者列表,但我混合了一些东西:在您的查询中,您已经过滤了特定年份的数据,因此该方法已过时。我假设您ur SqlDataReader获取所有行。@Rene--对,但如果我给出年份范围,即startyear和endyear@Rene--您可以在注释中添加代码行,以便读取年份范围数据
public List<Author> AuthorsOfYear(List<Author> fullList, int startyear, int endyear)
{
    return fullList.Where(author => 
          author.Year >= startyear && author.Year <= endyear).ToList();
}