如何将带有if条件的嵌套for循环转换为LINQ

时间:2016-07-18 13:25:31

标签: c# linq

我有一个糟糕的方法,从DataTable中提取数据并将其转换为所需的格式化DataTable。我确信在LinQ中有一个更好的方法可以做到这一点,但我并没有真正体验过它。如果有人能给我一个更好的解决方案,我将不胜感激。

继承代码

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);
            }
        }
    }

3 个答案:

答案 0 :(得分:0)

我会重写内部for循环

for (int x = 0; x < languages; x++)    
    foreach (DataRow row in dtExtracted.Rows)        
        importDt.Rows.Add
        (
           row[0], 
           Regex.Match(dtExtracted.Columns[x + 1].ToString(), @"\d+").Value; // LocaleIDs: Getting rid of non numeric characters from this column.
           row[x + 1]
        );

DataTable.Rows集合具有Add方法的重载,该方法接受一组对象:Add

答案 1 :(得分:0)

我不太确定LINQ在这里是否有任何帮助,但除了重写整个映射逻辑外,我至少会将此方法分成两部分:

   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++)
            {
                AddRow(importDt, dtExtracted, dtExtracted.Rows[j], x+1);
            }
        }
    }

    private static void AddRow(DataTable table, DataTable dtExtracted, DataRow originalRow, int language)
    {
        var row = table.NewRow();
        row[0] = originalRow[0];
        row[1] = Regex.Match(dtExtracted.Columns[language].ToString(), @"\d+").Value;
        row[2] = originalRow[language];
        table.Rows.Add(row);
    }

答案 2 :(得分:0)

你可以写这样的东西但是在这种情况下更像是滥用Linq但是如果你是出于教育目的而做的那样.. LINQ的主要功能是当你想枚举或过滤示例集合而不是像这样的情况

    public static void ExtractImportLayoutFromExcelDt(DataTable importDt, DataTable dtExtracted, int languages)
    {
        Enumerable.Range(0, languages)
            .ToList().ForEach(x =>
            {
                Enumerable.Range(0, dtExtracted.Rows.Count)
                    .ToList().ForEach(j =>
                    {
                        var row = dtExtracted.Rows[j];
                        DataRow tempRow = importDt.NewRow();
                        AddRow(importDt, dtExtracted, x, row, tempRow);
                    });
            });
    }

    private static void AddRow(DataTable importDt, DataTable dtExtracted, int x, DataRow row, DataRow tempRow)
    {
        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);
    }