简体   繁体   English

LINQ查询以具有枚举类型的类类型存储数据

[英]LINQ query to store data in a class type having enum type

I have have an Employee class like so: 我有一个像这样的Employee类:

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: 我正在使用以下LINQ查询:

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. System.NotSupportedException:LINQ to Entities无法识别方法'HMSTest.Gender ConvertToGender(System.String)'方法,并且该方法无法转换为商店表达式。

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. 我已经研究了此错误,并且我知道它不起作用,因为LINQ无法将用户定义的函数'Employee.ConvertToGender(string)'转换为等效的SQL查询,这也很有意义。 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. 我的意思是,这种功能(使用转换功能)在应用程序中非常常见,以至于Microsoft家伙一定已经想到了这一点。 I wanna know about something that I am entirely missing here. 我想知道我在这里完全不见的东西。

Linq to entities does not support enums in queries so there are a few ways you can go. Linq to实体不支持查询中的枚举,因此有几种方法可以使用。

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: 您可以将值作为字符串存储在Employee类上,然后将linq转换为对象并设置枚举值,如下所示:

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: 如果您想进一步简化,我建议将empGender的get修改为类似这样的方法,以避免第二次调用:

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM