我们的应用程序使用一个字符串来容纳用来指示枚举值的字符值。为前,枚举用于在表对准细胞:将char数组转换为枚举数组?
enum CellAlignment
{
Left = 1,
Center = 2,
Right = 3
}
和用于表示比对5列的表中的字符串:"12312"
。有没有一种简洁的方式来使用LINQ将此字符串转换为CellAlignment[] cellAlignments
?
这里就是我使出:
//convert string into character array
char[] cCellAligns = "12312".ToCharArray();
int itemCount = cCellAligns.Count();
int[] iCellAlignments = new int[itemCount];
//loop thru char array to populate corresponding int array
int i;
for (i = 0; i <= itemCount - 1; i++)
iCellAlignments[i] = Int32.Parse(cCellAligns[i].ToString());
//convert int array to enum array
CellAlignment[] cellAlignments = iCellAlignments.Cast<CellAlignment>().Select(foo => foo).ToArray();
...香港专业教育学院试过,但它说,指定的强制转换无效:
CellAlignment[] cellAlignmentsX = cCellAligns.Cast<CellAlignment>().Select(foo => foo).ToArray();
谢谢!
谢谢,这是超短的。由于枚举是基于整数,我相信显式转换比解析更好。 – mdelvecchio 2012-03-21 18:18:33