引用先前制作的对象

时间:2014-11-23 22:41:26

标签: c# object

在我的模特课上,我有一个像这样的教授和部门:

public class Professor
{
    public int ProfessorID { get; set; }
    public string Name { get; set; }
    public Department Department { get; set; }
    public bool Tenured { get; set; }
}

public class Department
{
    public int DepartmentID { get; set; }
    public string Name { get; set; }
    public List<Professor> Professors { get; set; }
}

我正在尝试制作种子方法,当我从之前创建的部门创建新教授时,我可以填充该部门。如何在SeedProfessor函数中引用先前创建的部门?

    private static List<Department> SeedDepartments()
    {
        var departments = new List<Department> 
        { 
            new Department{
                DepartmentID = 1,
                Name = "Psychology"
            }
        };
        return departments;
    }

    private static List<Professor> SeedProfessors()
    {
        var professors = new List<Professor> 
        { 
            new Professor{
                ProfessorID = 1,
                Name = "John Doe",
                Department = (***HERE IS WHERE I DON'T UNDERSTAND***)
                Tenured = true
            }
        };
        return professors;
    }

由于

3 个答案:

答案 0 :(得分:0)

我不确定这里的问题是什么。但你可以使用一个简单的LINQ来引用部门

var professors = new List<Professor> 
        { 
            new Professor{
                ProfessorID = 1,
                Name = "John Doe",
                Department = SeedDepartments().FirstOrDefault(),
                Tenured = true
            }
        };

OR

new Professor
          {
            ProfessorID = 1,
            Name = "John Doe",
            Department = SeedDepartments().Where(x => x.Name = "Psychology").FirstOrDefault(),
            Tenured = true
          }

答案 1 :(得分:0)

感谢你们两位回答。我还没有足够高的声誉数量来进行筹码交换。这一行:

  

Department = SeedDepartments()。FirstOrDefault(),

工作得很完美,但我也想尝试添加一个特定的部门。如果我这样做了:

  

部门= SeedDepartments()。其中​​(d =&gt; d.DepartmentID == 1),

我会收到一个错误说:

  

错误1无法隐式转换类型   'System.Collections.Generic.IEnumerable'到   'SRS.Models.Department'。存在显式转换(是你   错过演员?)D:\ Projects \ SRS \ SRS \ Models \ DbInitializer.cs 75 26 SRS

所以,我写了这个:

  

Department = SeedDepartments()。其中​​(x =&gt; x.Name ==   “心理学”)。FirstOrDefault(),

它有效。谢谢!

答案 2 :(得分:-1)

非常快速的解决方法:

private static List<Department> departments;
private static List<Professor> professors;

private static List<Department> SeedDepartments()
{
    if (departments == null)
    {
      departments = new List<Department> 
      { 
          new Department
          {
            DepartmentID = 1,
            Name = "Psychology"
          }
      };
    }

    return departments;
}

private static List<Professor> SeedProfessors()
{
    if (professors == null)
    {
       professors = new List<Professor> 
       { 
          new Professor
          {
            ProfessorID = 1,
            Name = "John Doe",
            Department = SeedDepartments().Where(d => d.DepartmentID == 1), //SeedDepartments()[0],
            Tenured = true
          }
       };
    }

    return professors;
}

假设部门在proffesors之前加载。

但我真的认为你真的应该把静态c&#39; tor 作为替代,如msdn所述:http://msdn.microsoft.com/en-us/library/k9x6w0hc.aspx