Yield Return Many?

后端 未结 6 676
粉色の甜心
粉色の甜心 2020-12-03 04:08

I find myself foreach-ing over IEnumerables a lot just so that I can return each result. Is there a way to compress something like this

foreach (var subSelec         


        
6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-03 04:46

    With C# 7.0, local functions are allowed, which enables us to have a fairly neat approach

    IEnumerable FlatEnumerable(){
        IEnumerable> NestedEnumerable(){
           yield return myEnumerable1;
           yield return myEnumerable2;
        }
    
        return NestedEnumerable().SelectMany(e => e);
    }
    

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/local-functions

提交回复
热议问题