DataTable에서 데이터를 추출하여 원하는 형식의 DataTable로 변환하는 끔찍한 방법이 있습니다. LinQ에서이 작업을 수행하는 데 훨씬 좋은 방법이있을 것이라고 확신하지만 실제로 사용하지는 않았습니다. 누군가가 내게 더 좋은 해결책을 제시 할 수 있다면 고맙겠습니다. 조건이있는 중첩 for 루프를 LINQ로 변환하는 방법
을 Heres 코드가private static void ExtractImportLayoutFromExcelDt(DataTable importDt, DataTable dtExtracted, int languages)
{
// The number of Locale colums included in the excel file.
for (int x = 0; x < languages; x++)
{
// The total number of friendlynames-keys/language included in the excel.
for (int j = 0; j < dtExtracted.Rows.Count; j++)
{
var row = dtExtracted.Rows[j];
DataRow tempRow = importDt.NewRow();
// Filling in the 3 columns. (FriendlyName - LocaleID - Text)
for (int i = 0; i <= 2; i++)
{
if (i == 0)
{
tempRow[i] = row[i]; // Friendly names: This is always going to be column 1 [0].
}
else if (i == 1)
{
tempRow[i] = Regex.Match(dtExtracted.Columns[x + 1].ToString(), @"\d+").Value; // LocaleIDs: Getting rid of non numeric characters from this column.
}
else
{
tempRow[i] = row[x + 1];
}
}
importDt.Rows.Add(tempRow);
}
}
}
초보자의 경우 이러한 부적절한 변수를 모두 읽는 것은 어렵습니다 (주석이 필요하면 다시 작성하십시오). 지금까지 뭐라구? –
Linq는'for'보다는'foreach'를 변환하는 것이 더 적합합니다. – juharr
당신은 가장 안쪽의'for' 루프가 필요하다고 확신합니까? – bashis