2017-02-24 64 views
0

我收到此错误,只有实体的无参数构造函数和初始值设定项?

只有参数构造函数初始化,并在LINQ 支持到实体。

经研究,这似乎是许多答案的常见问题。除了我无法逆向设计很多答案,我可以自己解决这个问题。

我意识到答案是在错误,但我不明白什么需要使用无参数构造函数?或者在这个例子中,我甚至不知道参数在哪里?

这LINQ查询给我找麻烦:

public static ObservableCollection<Employee> ReturnEmployeesInSection(string section) 
{ 
    using (StaffShiftManagerEntities dataBaseEntities = new StaffShiftManagerEntities()) 
    { 
      return new ObservableCollection<Employee>(dataBaseEntities.Staff_Data_TBL 
      .Where(p => p.Section_Data == section && p.Staff_Bool == true) 
      .Select(staff => new Employee(staff.Staff_No ?? -1, 
       staff.Staff_Name_First, staff.Staff_Name_Second)) 
      .ToList() 
      .GroupBy(staff => staff.Key) 
      .Select(staff => staff.First()) 
      .OrderBy(staff => staff.Key) 
      .ToList()); 
    } 
}  

而且Employee类:

public class Employee 
{ 
    public int Key { get; set; } 
    public string FirstName { get; set; } 
    public string SecondName { get; set; } 
    public string FullName => FirstName + " " + SecondName; 

    public Employee(int key, string first = null, string second = null) 
    { 
     Key = key; 
     FirstName = first; 
     SecondName = second; 
    } 
} 

而且这是实体框架自动生成的类:

public partial class Staff_Data_TBL 
{ 
    public long ID { get; set; } 
    public byte[] Image_Col { get; set; } 
    public Nullable<int> Staff_No { get; set; } 
    public string Staff_Name_First { get; set; } 
    public string Staff_Name_Second { get; set; } 
    public string Section_Data { get; set; } 
    public string Lic_Data { get; set; } 
    public Nullable<System.DateTime> Start_Date { get; set; } 
    public Nullable<System.DateTime> End_Date { get; set; } 
    public Nullable<long> Night_1 { get; set; } 
    public Nullable<long> Night_2 { get; set; } 
    public Nullable<long> Lunch_Data { get; set; } 
    public Nullable<long> Unples_Data { get; set; } 
    public string Staff_Part { get; set; } 
    public bool Staff_Kaizen { get; set; } 
    public int StaffKaizenPercentage { get; set; } 
    public Nullable<bool> Staff_Bool { get; set; } 
    public string Group_Section_Data { get; set; } 
    public string Notes { get; set; } 
} 

请原谅命名约定,我正在把所有事情都变成更加标准的过程命名。

回答

2

Employee类创建公共无参数构造函数。

public Employee() {} 

使用对象初始化,而不是构造函数参数:

 dataBaseEntities.Staff_Data_TBL 
     .Where(s => s.Section_Data == section && s.Staff_Bool == true) 
     .GroupBy(s => s.Staff_No) // Important, see notes below 
     .Select(g => g.FirstOrDefault()) 
     .OrderBy(s => s.Staff_No) 
     .Select(s => new Employee { 
      Key = s.Staff_No ?? -1, 
      FirstName = s.Staff_Name_First, 
      LastName = s.Staff_Name_Second 
      }) 
     .ToList() 

注意:正如你可以看到我感动分组和排序之前,结果投影。因此,您将在服务器端完成所有工作,而不是将表数据加载到内存中。

+1

这奏效了。非常感谢,我已经这么久了。 – KyloRen

+1

@KyloRen仔细使用'ToList()'它应该是你在查询中使用的最后一件事。否则,您会在过滤出您不需要的内容之前加载所有数据。 –

+0

非常感谢您的帮助,我帮助我理解了一直存在的问题,因为我不记得有多久(Especiallt与您最后一次注释)。我一定会对'ToList()'采取这种建议。令人惊叹的东西,再次感谢。 – KyloRen

0

变化

public class Employee 
{ 
    public int Key { get; set; } 
    public string FirstName { get; set; } 
    public string SecondName { get; set; } 
    public string FullName => FirstName + " " + SecondName; 

    public Employee(int key, string first = null, string second = null) 
    { 
     Key = key; 
     FirstName = first; 
     SecondName = second; 
    } 
} 

public class Employee 
{ 
    public Employee(){} //Constructor with no parameters that needs to be added 

    public int Key { get; set; } 
    public string FirstName { get; set; } 
    public string SecondName { get; set; } 
    public string FullName => FirstName + " " + SecondName; 

    public Employee(int key, string first = null, string second = null) 
    { 
     Key = key; 
     FirstName = first; 
     SecondName = second; 
    } 
} 
+0

不幸的是,我仍然得到与此相同的错误。 – KyloRen

相关问题