2017-02-24 41 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

相關問題