KyloRen
KyloRen

Reputation: 2741

Only parameterless constructors and initializers for Entities?

I am getting this error,

Only parameterless constructors and initializers are supported in LINQ to Entities.

Upon researching , this seems like a common problem with many answers. Except I cannot reverse engineer a lot of the answers so as I can solve this myself.

I realize the answer is in the error, but I can't see what needs to use parameterless constructors? Or for that matter in this example, I don't even know where the parameter even is???

This LINQ query giving me trouble:

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());
    }
}    

And the 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;
    }
}

And this is the class that Entity framework auto generated:

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; }
}

Please excuse the naming conventions, I am in the process of changing everything to more standard naming.

Upvotes: 0

Views: 1825

Answers (2)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236248

Create public parameterless constructor for Employee class.

public Employee() {}

Use object initializer instead of constructor with parameters:

     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()

NOTE: As you can see I moved grouping and sorting prior to results projection. Thus you will do all job on server side instead of loading table data into memory.

Upvotes: 3

Ervis Trupja
Ervis Trupja

Reputation: 2800

Change

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;
    }
}

to

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;
    }
}

Upvotes: 0

Related Questions