简体   繁体   English

枚举值强制转换/转换为 Int32 c#

[英]Enum value casted / converted to Int32 c#

I have this existing Enum that is used in different classes it goes similar to this:我有这个现有的 Enum 用于不同的类,它类似于:

 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 .问题是,我正在制作一个包含 3 个表的联合的存储过程,其中我包含了一个附加列,该列的值仅被查询以确定它是从哪个表中检索的。 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.列类型映射到 Enum1。 Since I had only provided the value of Type in the query it's default value is int / Int32.因为我只在查询中提供了 Type 的值,所以它的默认值是 int / Int32。 This Where I encounter a System.InvalidCastException: 'Unable to cast object of type 'System.Int32' to type 'System.Byte'.'这是我遇到 System.InvalidCastException 的地方:'无法将类型为'System.Int32'的对象转换为类型'System.Byte'。 I cannot change the dataType of my Enum since it is used in other classes.我无法更改 Enum 的 dataType,因为它在其他类中使用。

I have tried to cast/convert Type into Byte but gives another error.我曾尝试将 Type 转换/转换为 Byte 但给出了另一个错误。 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 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 .而我的 Enum1 是一个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 .我的主要问题是如何将 Column 'Type' 从Int32转换为Byte ,后者映射到我的 Enum1 ,其数据类型为Byte And adding a CAST Operation in my DB Query solved the problem.在我的数据库查询中添加一个 CAST 操作解决了这个问题。

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.起初,我试图在我的班级中转换“Type”的传递值,但解决我的数据库查询中的问题解决了它。 Now, I don't have to convert the column Type in my class or my Enum1 to Int32 .现在,我不必将班级或 Enum1 中的列 Type 转换为Int32

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

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