简体   繁体   中英

Enum value casted / converted to Int32 c#

I have this existing Enum that is used in different classes it goes similar to this:

 public Enum Enum1: byte
{
    [Description("Table 1")] tbl1= 0,
    [Description("Table 2")] tbl2= 1,
    [Description("Table 3")] tbl3= 2
}

It works perfectly fine when used in classes that maps its properties to the Database. The problem is, I am making a stored procedure that contains a Union of 3 Tables where I included a additional column whose value is only queried to determine what table it is retrieved from . Example query :

SELECT id AS ID, name AS Name, '0' AS Type
FROM tbl1
UNION
SELECT id AS ID, name AS Name, '1' AS Type
FROM tbl2
UNION
SELECT id AS ID, name AS Name, '2' AS Type
FROM tbl3

So the results be :

ID | Name   | Type
1    Jose      0
1    Admins    1
1    Visitors  2

The Column Type is mapped to Enum1. Since I had only provided the value of Type in the query it's default value is int / Int32. This Where I encounter a System.InvalidCastException: 'Unable to cast object of type 'System.Int32' to type 'System.Byte'.' I cannot change the dataType of my Enum since it is used in other classes.

I have tried to cast/convert Type into Byte but gives another error. Code snippet below

 public Enum1 Type { get { return Convert.ToByte(Type)}; set { Type = Convert.ToInt32(value)}; }
 [NotMapped]
 public string TypeName 
 {
   get { return Type.GetAttributeOfType<DescriptionAttribute>().Description; }
   private set { Type = value.ToEnum<RecipientType>(); }
 }

Is there a way to solve this or a better way to do this? TIA

Sorry for the trouble and being vague about it. I figured how to solve my problem. So here is a further explanation. I have a model class that receives the result of my query with the columns:

ID BIGINT
Name VARCHAR (250)
Type --> queried

My Model:

public class Model1 
{
  public int ID {get;set;}
  public string Name {get;set;}
  public Enum1 Type { get ; set ; }
  [NotMapped]
  public string TypeName 
  {
   get { return Type.GetAttributeOfType<DescriptionAttribute>().Description; }
   set { Type = value.ToEnum<RecipientType>(); }
  }    
}

And My Enum1 was a byte .

My main problem was how to convert the Column 'Type' from Int32 to Byte which is mapped to my Enum1 whose data type is a Byte . And adding a CAST Operation in my DB Query solved the problem.

SELECT id AS ID, name AS Name, CAST (0 AS TINYINT) AS Type
FROM tbl1
UNION
SELECT id AS ID, name AS Name, CAST (1 AS TINYINT) AS Type
FROM tbl2
UNION
SELECT id AS ID, name AS Name, CAST (2 AS TINYINT) AS Type
FROM tbl3

At first I am trying to convert the passed value of 'Type' in my class but solving the problem in my DB query solved it. Now, I don't have to convert the column Type in my class or my Enum1 to Int32 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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