只有实体的无参数构造函数和初始值设定项?
Only parameterless constructors and initializers for Entities?
我收到这个错误,
Only parameterless constructors and initializers are supported in LINQ
to Entities.
经过研究,这似乎是一个有很多答案的常见问题。除了我不能对很多答案进行逆向工程,所以我可以自己解决这个问题。
我意识到答案在错误中,但我看不到什么需要使用无参数构造函数?或者在这个例子中,我什至不知道参数在哪里???
这个 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
class:
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;
}
}
这是 Entity framework 自动生成的 class:
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; }
}
请原谅命名约定,我正在将所有内容更改为更标准的命名。
为 Employee
class 创建 public 无参数构造函数。
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()
注意:如您所见,我在结果投影之前移动了分组和排序。因此,您将在服务器端完成所有工作,而不是将 table 数据加载到内存中。
改变
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;
}
}
我收到这个错误,
Only parameterless constructors and initializers are supported in LINQ to Entities.
经过研究,这似乎是一个有很多答案的常见问题。除了我不能对很多答案进行逆向工程,所以我可以自己解决这个问题。
我意识到答案在错误中,但我看不到什么需要使用无参数构造函数?或者在这个例子中,我什至不知道参数在哪里???
这个 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
class:
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;
}
}
这是 Entity framework 自动生成的 class:
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; }
}
请原谅命名约定,我正在将所有内容更改为更标准的命名。
为 Employee
class 创建 public 无参数构造函数。
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()
注意:如您所见,我在结果投影之前移动了分组和排序。因此,您将在服务器端完成所有工作,而不是将 table 数据加载到内存中。
改变
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;
}
}