linq 简化或重构嵌套foreach循环

zbdgwd5y  于 2023-01-15  发布在  其他
关注(0)|答案(1)|浏览(212)

我是新的c#和尝试简化下面的代码
有人能帮我简化这段代码而不破坏代码流吗?
下面是我在嵌套级别上有5个foreach循环的代码片段。
任何帮助都将不胜感激。

var rows = new List<TBRow>();
        foreach (var rowData in _inputData.TBFlat)
        {
            foreach (var data in rowData)
            {
                if (data.Details != null)
                {
                    foreach (var tLevel in data.Details)
                    {
                        if (tLevel.Details != null)
                        {
                            foreach (var ruLevel in tLevel.Details)
                            {
                                if (ruLevel.Details != null)
                                {
                                    foreach (var acLevel in ruLevel.Details)
                                    {
                                        row = //Operation1
                                        rows.Add(row);
                                    }
                                }
                                else
                                {
                                    row = //Operation2
                                    rows.Add(row);
                                }
                            }
                        }
                        else
                        {
                            row = //Operation3
                            rows.Add(row);
                        }
                    }
                }
                else
                {
                    row = //Operation4
                    rows.Add(row);
                }
            }
        }
mwyxok5s

mwyxok5s1#

很难猜测你需要什么,可以做什么(没有操作和样本数据),也许这能帮助你:

class SimplifyOrRefactoredNestedForeachLoops {
    public class TBRow {}

    public interface IGetTBRow {
        TBRow GetRow();
    }        
    
    public abstract class LvlWithDetails<T> :IGetTBRow {
        public T[] Details;

        public virtual TBRow GetRow() {
            throw new NotImplementedException();
        }
    }

    public class Lvl1 :LvlWithDetails<Lvl2> {}

    public class Lvl2 :LvlWithDetails<Lvl3> {}

    public class Lvl3 :LvlWithDetails<Lvl4> {}

    public class Lvl4 :IGetTBRow {
        public TBRow GetRow() {
            throw new NotImplementedException();
        }
    }

    public class Tinput {
        public Lvl1[][] TBFlat;
    }

    IEnumerable<TBRow> GetIDontKnowWhatButFlat(Tinput _inputData) {
        foreach(var rowData in _inputData.TBFlat) {
            foreach(var data in rowData) {
                if(data.Details != null) {
                    foreach(var tLevel in data.Details) {
                        if(tLevel.Details != null) {
                            foreach(var ruLevel in tLevel.Details) {
                                if(ruLevel.Details != null) {
                                    foreach(var acLevel in ruLevel.Details) {
                                        yield return acLevel.GetRow();
                                    }
                                } else {
                                    yield return ruLevel.GetRow();
                                }
                            }
                        } else {
                            yield return tLevel.GetRow();
                        }
                    }
                } else {
                    yield return data.GetRow();
                }
            }
        }
    }
}

相关问题