Is there any way to get some list of all Sql Data Types on c#?
I know that we have a Enum called SQlDbType which contains all Sql Data Types, but can we convert an Enum to an array of strings?
I only want to get some list with all Sql Data Types, instead开发者_C百科 of writing them one by one and adding them into my array of strings.
Enum to an array of strings is pretty easy:
var names = Array.ConvertAll((SqlDbType[]) Enum.GetValues(typeof(SqlDbType)),
type => type.ToString());
or using LINQ:
var names = Enum.GetValues(typeof(SqlDbType))
.Cast<SqlDbType>()
.Select(x => x.ToString())
.ToArray();
Or more pleasantly using my Unconstrained Melody library, either as an array or (more efficiently) an immutable list:
string[] names = Enums.GetNamesArray<SqlDbType>();
IList<string> namesList = Enums.GetNames<SqlDbType>();
精彩评论