如图所示这个竞卖状态,原先是在前端界面通过html代码写死的几个状态,现在需要改为动态加载。这个几个状态是定义的枚举类型。

1:定义一个枚举类型

/// <summary>
    /// 资源状态
    /// </summary>
    public enum ResourceState
    {
        /// <summary>
        /// 下架
        /// </summary>
        [Description("下架")]
        SoldOut = 0,
        /// <summary>
        /// 上架
        /// </summary>
        [Description("上架")]
        Putaway = 1,

/// <summary>
        /// 交易成功
        /// </summary>
        [Description("交易成功")]
        Success = 2,

/// <summary>
        /// 废标
        /// </summary>
        [Description("废标")]
        AbandonedTender = 6,
        /// <summary>
        /// 违约标
        /// </summary>
        [Description("违约标")]
        DefaultMark = 7,

/// <summary>
        /// 中标
        /// </summary>
        [SetClassification(Type = 5)]
        [Description("中标")]
        WinTheBidding = 3,
        /// <summary>
        /// 流标
        /// </summary>
        [SetClassification(Type = 6)]
        [Description("流标")]
        FlowStandard = 4,
        /// <summary>
        /// 未中标
        /// </summary>
        [SetClassification(Type = 4)]
        [Description("未中标")]
        LoseABid = 5,
        /// <summary>
        /// 竞价中
        /// </summary>
        [SetClassification(Type = 2)]
        [Description("竞价中")]
        Bidding = 8,

/// <summary>
        /// 竞拍中
        /// </summary>
        [SetClassification(Type = 3)]
        [Description("竞拍中")]
        Auctioning = 9,

/// <summary>
        /// 已处理(针对于流标资源)
        /// </summary>
        [Description("已处理")]
        Alreadyprocessed = 10,

/// <summary>
        /// 已过期
        /// </summary>
        [Description("已过期")]
        ExpiredTime = 11,

/// <summary>
        /// 所有报价
        /// </summary>
        [SetClassification(Type = 1)]
        [Description("所有报价")]
        All = 12

}

2:自定义一个标记类型继承Attribute

/// <summary>
    /// 添加自定义属性

/// 作用:过滤枚举类型
    /// </summary>
    public class SetClassificationAttribute : Attribute
    {
        /// <summary>
        /// 分类
        /// </summary>
        public int Type { get; set; }

public SetClassificationAttribute() { }

}

3:自定义返回List类型

/// <summary>
    /// 自定义返回值类型
    /// </summary>
    public class EnumberCreditType
    {
        /// <summary> 
        /// 枚举的描述 
        /// </summary> 
        public string Desction { set; get; }

/// <summary> 
        /// 枚举名称 
        /// </summary> 
        public string Key { set; get; }

/// <summary> 
        /// 枚举对象的值 
        /// </summary> 
        public int Value { set; get; }

/// <summary>
        /// 描述
        /// </summary>
        public string Name { get; set; }

/// <summary>
        /// 分类
        /// </summary>
        public int Classification { set; get; }
    }

4:枚举转为List<EnumberCreditType>方法

/// <summary>
        /// 获取枚举列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<EnumberCreditType> EnumToList<T>()
        {
            List<EnumberCreditType> list = new List<EnumberCreditType>();

foreach (var e in Enum.GetValues(typeof(T)))
            {
                EnumberCreditType m = new EnumberCreditType();
                object[] objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
                if (objArr != null && objArr.Length > 0)
                {
                    DescriptionAttribute da = objArr[0] as DescriptionAttribute;
                    m.Desction = da.Description;
                }
                //SetClassification 
                object[] setClassificationArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(SetClassificationAttribute), true);
                if (setClassificationArr != null && setClassificationArr.Length > 0)
                {
                    SetClassificationAttribute da = setClassificationArr[0] as SetClassificationAttribute;
                    m.Classification = da.Type;
                }
                //Display
                object[] disArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DisplayAttributes), true);
                if (disArr != null && disArr.Length > 0)
                {
                    DisplayAttribute da = disArr[0] as DisplayAttribute;
                    m.Name = da.Name;
                }

m.Value = Convert.ToInt32(e);
                m.Key = e.ToString();
                list.Add(m);
            }
            return list;
        }

5:使用

static void Main(string[] args)
        {

// 获取枚举类型转为List,使用List的Where过滤条件
            var query =  _enumAppservice.EnumToList<ResourceState>().Where(e => e.Classification>=1&&e.Classification<=6).OrderBy(e=>e.Classification).ToList();
            Console.ReadKey();
        }

.NET枚举类型转为List类型的更多相关文章

  1. MySQL中,把varchar类型转为date类型

    如下表: 先使用str_to_date函数,将其varchar类型转为日期类型,然后从小到大排序 语法:select str_to_date(class_time,'%Y%m%d %H:%i:%s') ...

  2. 使用alibaba的json工具将String类型转为JSONArray类型

    转化流程:先将输入流转为String类型,再使用alibaba的json转换工具,将字符串转化为json数组 SensorDevices sensorDevices = new SensorDevic ...

  3. c++中 string类型 转为 char []类型

    将string类型转换为字符数组char [] char arr[50]; //数组大小根据s的大小确定 string s= "12slfjksldkfjlsfk"; int le ...

  4. js 字符串类型转为数组类型

    以前从来没有想过这个转换,以为直接拼出来就可以了,今天同事问我这个问题,特记录如下. var test='["colkey", "col", "col ...

  5. linq to sql ,将var 类型转为 IList 类型

    public void SOHSelecting(int startRowIndex, int maximumRows, string sortExpression, string location) ...

  6. django:将query-set类型转为json类型

    import json data = json.dumps(list(my_table.objects.all().values())) return HttpResponse(data)

  7. java Int类型转为double 类型;string转double

    int a=12; double b=(double)a; or double c=Double.valueOf((double)a); string a_s="12"; doub ...

  8. 【C#】string格式的日期转为DateTime类型及时间格式化处理方法

    日期格式:yyyyMMdd HH:mm:ss(注意此字符串的字母大小写很严格) yyyy:代表年份 MM: 代表月份 dd: 代表天 HH: 代表小时(24小时制) mm: 代表分钟 ss: 代表秒 ...

  9. 前端String类型转JSON类型

    在js中通过ajax获取数据后,赋值给前端树形插件zTree中的zNodes,但一直失败如图: 浏览器端报错: 于是我将zNodes变量的值直接写死,并打印这两个的值,如图 再次运行成功.浏览器控制台 ...

随机推荐

  1. Spring+SpringMVC+MyBatis+easyUI整合基础篇(九)版本控制

    日常啰嗦 还好在第一篇文章里就列好了接下来的主线及要写的知识点,不然都不知道要写什么东西了,开篇里已经列了基础篇要讲svn和git的知识点,所以这一篇就写一下版本控制. 项目实际效果展示在这里,账密: ...

  2. 3389: [Usaco2004 Dec]Cleaning Shifts安排值班

    3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 102  Solved: ...

  3. app 评分

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #822e0e } p.p2 { margin: 0.0px 0. ...

  4. git remote log error

    使用git pull的时候收到以下信息: error: there are still refs under 'refs/remotes/origin/xxxx'From 10.1.25.57:yyy ...

  5. pom.xml配置文件配置jar(不用记,快速配置)

    1:网址:http://mvnrepository.com/ 2:在搜索栏搜索要用的框架;例如spring *以下为示例

  6. 2.sparkSQL--DataFrames与RDDs的相互转换

    Spark SQL支持两种RDDs转换为DataFrames的方式 使用反射获取RDD内的Schema     当已知类的Schema的时候,使用这种基于反射的方法会让代码更加简洁而且效果也很好. 通 ...

  7. CentOS 安装 clamav

    1.配置yum源 # cd /etc/yum.repos.d/ # vi dag.repo写入下列内容 #Dag RPM Repository Start[dag]name=Dag RPM Repos ...

  8. 【Electron】Electron开发入门(六):项目生成setup安装程序

    把electron发布的exe打包成setup安装程序,需要使用nsis软件, nsis打包的详细教程,可以参考我的这篇文章: win7下nsis打包exe安装程序教程

  9. 手机自动化测试:appium源码分析之bootstrap六

    手机自动化测试:appium源码分析之bootstrap六   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.poptest测试 ...

  10. Angularjs 实现移动端在线测评效果

    注:此文所用的angular版本为 1.6 一.运行效果图 二.需求 1. 点击选项时,背景变为黄色(即选中状态),并且自动切换到下一题 2. 切换到下一题时,顶部进度随之改变 3. 选中时要把对应的 ...