Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 要枚举的泛型列表_C#_List_Enums - Fatal编程技术网

C# 要枚举的泛型列表

C# 要枚举的泛型列表,c#,list,enums,C#,List,Enums,我有以下课程 public partial class ActionType { public System.Guid ActionTypeId { get; set; } public string ActionTypeName { get; set; } } 现在我想使用数据库中存储的所有ActionType,如下所示: MyEntity entity = new MyEntity(); entity.ActionTypeId = ActionTypes.??? 在这里

我有以下课程

public partial class ActionType
{
    public System.Guid ActionTypeId { get; set; }
    public string ActionTypeName { get; set; }
}
现在我想使用数据库中存储的所有ActionType,如下所示:

MyEntity entity = new MyEntity();
entity.ActionTypeId = ActionTypes.??? 
在这里,我希望能够通过ActionTypeName选择ActionTypeId

现在我这样做:

public class ActionTypes
{
    public static readonly Guid CustomerAdded = new Guid("36520b53-e1d0-4c96-bec8-0df85536a96b");
    public static readonly Guid CustomerEdited = new Guid("732592d3-7423-4250-aa74-e10fe1e7c030");
}
我希望能够从数据库中创建ActionTypes programmaticaly,而不是自己编写值

所以我想做一些类似的事情:

public class ActionTypes
{
   List<ActionType> list = context.GetAllActionTypes();
   foreach(ActionType type in list)
   {
      public static readony Guid type.ActionTypeName = type.ActionTypeId;
   }
}
我如何实现我的意图


最好的问候

也许是一个字典查找表,地图是你需要的吗

public class ActionTypes
{
   private List<ActionType> list = context.GetAllActionTypes();
   private Dictionary<string, Guid> ActionTypesMap { get; private set; }

   public ActionTypes() {
     this.ActionTypesMap = list.ToDictionary(
       k => k.ActionTypeName,
       v => v.ActionTypeId);
   }
}

访问方式:Guid addedGuid=new ActionTypes.ActionTypesMap[CustomerAdded]

您可以使用T4模板在编译时生成文件。这还具有保持智能感知和验证成员名称的额外好处

大概是这样的:

MyEntity entity = new MyEntity();
entity.ActionTypeId = ActionTypes.??? 
ActionTypes.tt:

<#@ template   language    = "C#"                           #>
<#@ assembly   name        = "Microsoft.CSharp"             #>
<#@ assembly   name        = "System.Core"                  #>
<#@ assembly   name        = "System.Data"                  #>
<#@ import     namespace   = "System.Collections.Generic"   #>
<#@ import     namespace   = "System.Dynamic"               #>
<#@ import     namespace   = "System.Linq"                  #>
<#@ import     namespace   = "System.Data.SqlClient"        #>

public class ActionTypes 
{
<#
var con = new SqlConnection("Data Source=localhost\SQLExpress;Initial Catalog=MyDatabaseName;Integrated Security=True");
con.Open();

var sqc = new SqlCommand("SELECT ActionTypeID, ActionTypeName FROM ActionTypes", con);
var reader = sqc.ExecuteReader();

using (var reader = sqc.ExecuteReader())
{
    while (reader.Read())
    {
#>
    public static Guid <#=reader.GetString(reader.GetOrdinal("ActionTypeName"))#> = new Guid("<#=reader.GetGuid(reader.GetOrdinal("ActionTypeID"))#>");
<#
    }
}
con.Close();
#>
}

我不太明白。字典有用吗?你在这里问的问题实在是太不清楚了。你的代码很有效,你想要什么不同呢?抱歉,伙计们,我已经编辑了这个问题,我可以通过以下操作实现同样的效果:Guid addedGuid=list.FirstOrDefaultx=>x.ActionTypeName==CustomerAdded.ActionTypeId;那么,您希望如何引用您的动作类型?你需要知道这个名字。我知道你可以用FirstOrDefault来做,dictionary速度更快,但占用更多内存。