获取错误“提供的类型必须是枚举。” [英] Getting Error "Type provided must be an Enum."

查看:112
本文介绍了获取错误“提供的类型必须是枚举。”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 GeneralSelectServiceClient objservice =  new  GeneralSelectServiceClient(); 
IEnumerable< clist> objApproverlist = objservice.GetCList(txtbox.Text);

var result = 来自 CList cu in objApproverlist
选择 new {CCode =( int )Enum.Parse( typeof (CList),cu.ToString()),CDescription = cu.ToString()} ; // 此行

var tempValue = new {CCode = 0 ,CDescription = - 选择 - };

var list = result.ToList(); // 创建可变列表

list.Insert( 0 ,tempValue);

cbCurrency.DisplayMember = CDescription;
cbCurrency.ValueMember = CCode
cbCurrency.DataSource = objApproverlist.ToList ();

解决方案

 CCode =( int )Enum.Parse( typeof (EnumHere),cu.ToString())





Enum.Parse需要枚举类型。你正在解析它是一个可枚举的CList



更新:用户通知CList是Enum。



ok - if

 IEnumerable< clist> objApproverlist = objservice.GetCList(txtbox.Text); 



实际上是

 IEnumerable< CList> objApproverlist = objservice.GetCList(txtbox.Text); 



那么你不需要那种长篇大论来获得int:

 var result =来自CList cu in objApproverlist select new {CCode =(int)cu,CDescription = cu.ToString()}; 





但我已经测试了两种方式,它对我有用。



请告诉我们您正在使用的所有物品类型



更新2:

我的测试代码:< br $>


  public   enum  CList 
{
a,b,c,d
}


private 静态 void Main()
{

IEnumerable< CList> ; objApproverlist = Enum.GetValues( typeof (CList))。Cast< CList>()。AsEnumerable();

var result = 来自 CList cu in objApproverlist
选择 new {
CCode =( int )Enum.Parse( typeof (CList),cu.ToString()),
// 我也尝试过:
/ / CCode =(int)cu,
CDescription = cu.ToString()};

var tempValue = new {CCode = 0 ,CDescription = - 选择 - };

var list = result.ToList();

list.Insert( 0 ,tempValue);


list.ForEach(a = >
{
Console.WriteLine( {0} \t {1},a.CCode,a.CDescription);
});
}





这两种情况都适用。我得到了读数{0 a},{1 b}等


你已经创建了列表但是将其他东西分配为数据源!我认为你需要下面的代码

 cbCurrency.DataSource = list; 


GeneralSelectServiceClient objservice = new GeneralSelectServiceClient();
            IEnumerable<clist> objApproverlist = objservice.GetCList(txtbox.Text);

 var result = from CList cu in objApproverlist
                             select new { CCode = (int)Enum.Parse(typeof(CList), cu.ToString()), CDescription = cu.ToString() }; //this line

                var tempValue = new { CCode = 0, CDescription = "-- Select --" };

                var list = result.ToList(); // Create mutable list

                list.Insert(0, tempValue);

  cbCurrency.DisplayMember = "CDescription"; 
            cbCurrency.ValueMember = "CCode" 
            cbCurrency.DataSource = objApproverlist.ToList();

解决方案

CCode = (int)Enum.Parse(typeof(EnumHere), cu.ToString())



Enum.Parse requires an enum type. You are parsing it an enumerable CList

UPDATE: User informs that CList is Enum.

ok - if

IEnumerable<clist> objApproverlist = objservice.GetCList(txtbox.Text);


is actually

IEnumerable<CList> objApproverlist = objservice.GetCList(txtbox.Text);


then you don't need that long winded way of getting the int:

var result = from CList cu in objApproverlist select new { CCode = (int)cu, CDescription = cu.ToString() };



But I have tested both ways and it works for me.

Please tell us all the types of the objects you're using

UPDATE 2:
My Test code:

public enum CList
{
    a, b, c, d
}


private static void Main()
{

    IEnumerable<CList> objApproverlist = Enum.GetValues(typeof(CList)).Cast<CList>().AsEnumerable();

    var result = from CList cu in objApproverlist
                 select new { 
                        CCode = (int)Enum.Parse(typeof(CList), cu.ToString()),
                      //I also tried with:
                      //CCode = (int)cu,
                        CDescription = cu.ToString() }; 

    var tempValue = new { CCode = 0, CDescription = "-- Select --" };

    var list = result.ToList(); 

    list.Insert(0, tempValue);


    list.ForEach(a =>
    {
        Console.WriteLine("{0}\t{1}", a.CCode, a.CDescription);
    });
}



This works in both cases. I get the readout {0 a}, {1 b}, etc.


you have created list but assign something else as datasource!, I think you need below code

cbCurrency.DataSource = list; 


这篇关于获取错误“提供的类型必须是枚举。”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆