GaganLamba
GaganLamba

Reputation: 1

LINQ query to store data in a class type having enum type

I have have an Employee class like so:

public class Employee    
{  
    public Int32 employeeId;    
    public String employeeFName;    
    public String employeeSName;    
    public Gender empGender;    
    public string empContactNo;    
    public DateTime empDOB;    
    public string empAddress;    
    public Int16 accessLevel;    
    private string pass;    


    public String Pass
    {
        get { return this.pass; }
        set { this.pass = value; }
    }

    public static Gender ConvertToGender(string gen)
    {
        if (gen == "Male")
            return Gender.Male;
        else
            return Gender.Female;
    }
}

Where 'Gender' is of type Enum:

public enum Gender { Male, Female }    

I am using the following LINQ query:

var query = from emp in hmsdatabase.TblEmployees  
               where emp.EmpId == employeeid  
               select new Employee()  
               {  
                    employeeId = emp.EmpId,  
                    employeeFName = emp.EmpFirstName,  
                    employeeSName = emp.EmpSurName,  
                    empGender = Employee.ConvertToGender(emp.EmpGender),  
                    empContactNo = emp.EmpContactNo,  
                    empDOB = DateTime.Parse(emp.EmpDOB.ToString()),  
                    empAddress = emp.EmpAddress,  
                    accessLevel = Int16.Parse(emp.EmpAccessRight.ToString())  
               };  

Although there is no error during compilation,I am getting the following error during runtime:

System.NotSupportedException: LINQ to Entities does not recognize the method 'HMSTest.Gender ConvertToGender(System.String)' method, and this method cannot be translated into a store expression.

I've researched regarding this error and I know that it doesn't work because LINQ cannot convert the user defined function 'Employee.ConvertToGender(string)' into an equivalent SQL query and It makes sense too. So is there any easy workaround?? I mean this kind of functionality(using conversion functions) is so common in applications, that microsoft guys must have thought of this. I wanna know about something that I am entirely missing here.

Upvotes: 1

Views: 1207

Answers (1)

Tj Kellie
Tj Kellie

Reputation: 6476

Linq to entities does not support enums in queries so there are a few ways you can go.

You can store the value as a string on the Employee class and then convert to linq to objects and set the enum value like this:

var objEmpl = (from emp in hmsdatabase.TblEmployees  
           where emp.EmpId == employeeid  
           select new Employee()  
           {  
                employeeId = emp.EmpId,  
                employeeFName = emp.EmpFirstName,  
                employeeSName = emp.EmpSurName,  
                empGenderString = emp.EmpGender,  
                empContactNo = emp.EmpContactNo,  
                empDOB = DateTime.Parse(emp.EmpDOB.ToString()),  
                empAddress = emp.EmpAddress,  
                accessLevel = Int16.Parse(emp.EmpAccessRight.ToString())  
           } ).FirstOrDefault();
    objEmpl.empGender = Employee.ConvertToGender(objEmpl.empGenderString);

If you wanted to simplify further I would suggest modifying the get on empGender to something like this to avoid the 2nd call:

public class Employee    
{  
  public Int32 employeeId;    
  public String employeeFName;    
  public String employeeSName;         
  public string empGenderString;
  public string empContactNo;    
  public DateTime empDOB;    
  public string empAddress;    
  public Int16 accessLevel;    
  private string pass;    

public Gender empGender
{
    get { return this.empGenderString == "Male" ?
                 Gender.Male:
                 Gender.Female;
        }

public String Pass
{
    get { return this.pass; }
    set { this.pass = value; }
}

}

Upvotes: 1

Related Questions