. NET Enumeration type to List type,. net enumeration to list type
This bidding status was originally written to several dead States through html code on the front-end interface. Now it needs to be changed to dynamic loading. These statuses are defined as enumeration types.
1: Define an enumeration type
/// <Summary>
/// Resource Status
/// </Summary>
Public enum ResourceState
{
/// <Summary>
/// Dismounting
/// </Summary>
[Description ("dismounting")]
SoldOut = 0,
/// <Summary>
/// Mounting
/// </Summary>
[Description ("shelving")]
Putaway = 1,
/// <Summary>
/// Transaction successful
/// </Summary>
[Description ("transaction successful")]
Success = 2,
/// <Summary>
/// Waste mark
/// </Summary>
[Description ("Waste mark")]
AbandonedTender = 6,
/// <Summary>
/// Default mark
/// </Summary>
[Description ("default")]
DefaultMark = 7,
/// <Summary>
/// Winning
/// </Summary>
[SetClassification (Type = 5)]
[Description ("")]
WinTheBidding = 3,
/// <Summary>
/// Stream mark
/// </Summary>
[SetClassification (Type = 6)]
[Description ("streaming")]
FlowStandard = 4,
/// <Summary>
/// Fail to win
/// </Summary>
[SetClassification (Type = 4)]
[Description ("not winning")]
LoseABid = 5,
/// <Summary>
/// Bidding
/// </Summary>
[SetClassification (Type = 2)]
[Description ("Bidding")]
Bidding = 8,
/// <Summary>
/// Bidding
/// </Summary>
[SetClassification (Type = 3)]
[Description ("Bidding")]
Auctioning = 9,
/// <Summary>
/// Processed (for streaming Resource)
/// </Summary>
[Description ("processed")]
Alreadyprocessed = 10,
/// <Summary>
/// Expired
/// </Summary>
[Description ("expired")]
ExpiredTime = 11,
/// <Summary>
/// All quotations
/// </Summary>
[SetClassification (Type = 1)]
[Description ("All quotations")]
All = 12
}
2: Customize a tag type inheritanceAttribute
/// <Summary>
/// Add custom attributes
/// Function: Filter enumeration types
/// </Summary>
Public class SetClassificationAttribute: Attribute
{
/// <Summary>
/// Category
/// </Summary>
Public int Type {get; set ;}
Public SetClassificationAttribute (){}
}
3: Custom return List type
/// <Summary>
/// Custom Return Value Type
/// </Summary>
Public class EnumberCreditType
{
/// <Summary>
/// Enumeration description
/// </Summary>
Public string Desction {set; get ;}
/// <Summary>
/// Enumeration name
/// </Summary>
Public string Key {set; get ;}
/// <Summary>
/// Enumerated object Value
/// </Summary>
Public int Value {set; get ;}
/// <Summary>
/// Description
/// </Summary>
Public string Name {get; set ;}
/// <Summary>
/// Category
/// </Summary>
Public int Classification {set; get ;}
}
4: Convert enumeration to List <EnumberCreditType>
/// <Summary>
/// Obtain the enumerated list
/// </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: Use
Static void Main (string [] args)
{
// Convert the retrieved enumerated type to List and use the Where filter condition of List
Var query = _ enumAppservice. enumToList <ResourceState> (). where (e => e. classification> = 1 & e. classification <= 6 ). orderBy (e => e. classification ). toList ();
Console. ReadKey ();
}