I have the following Enum:
public enum QuestionType {
Check = 1,
CheckAndCode = 2,
na = 99
};
public static class QuestionTypeExtension
{
public static string D2(this QuestionType key)
{
return ((int) key).ToString("D2");
}
}
I already created an extension method that formats the output but now I have another requirement. What I need to do is to create an extension method that will return the contents of the enum into a list of the following class:
public class Reference {
public string PartitionKey { get; set; } // set to "00"
public int RowKey { get; set; } // set to the integer value
public string Value { get; set; } // set to the text of the Enum
}
Is it possible to do this in an extension method?
Try the following:
public static List<Reference> GetReferencesForQuestionType()
{
return Enum.GetValues(typeof(QuestionType))
.Cast<QuestionType>()
.Select(x => new Reference
{
PartitionKey = "00",
RowKey = (int)x,
Value = x.ToString()
})
.ToList();
}
If you want to create an instance of the Reference
-class for just one element in an extenstion method try this:
public static Reference ToReference(this QuestionType questionType)
{
return new Reference
{
PartitionKey = "00",
RowKey = (int)questionType,
Value = questionType.ToString()
};
}
How about...
public static class QuestionTypeExtension
{
public static IEnumerable<Reference> Reference()
{
return Enum.GetValues(typeof(QuestionType)).OfType<QuestionType>().
Select(qt=>new Reference(){ PartitionKey = "00", RowKey = (int)qt, Value = qt.ToString()});
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.