우리 앱은 문자열을 사용하여 열거 형 값을 나타내는 데 사용되는 문자 값을 저장합니다. 예를 들어, 테이블의 셀을 정렬하기위한 열거 :char 배열을 enum 배열로 변환 하시겠습니까?
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();
당신을 감사합니다!
string input = "12312";
CellAlignment[] cellAlignments = input.Select(c => (CellAlignment)Enum.Parse(typeof(CellAlignment), c.ToString()))
.ToArray();
감사합니다. 이것은 매우 짧습니다. enum은 int를 기반으로하므로 명시 적으로 변환하는 것이 구문 분석하는 것보다 낫다고 생각합니다. – mdelvecchio