如何清理此LINQ查询(SelectMany)?

mko*_*ski 3 c# linq linq-to-objects

如何清理此LINQ查询以在sql语法中使用SelectMany,而不是像我一样在最后进行方法链接?

 var runPeakWidths =
     (from ipa in runAnalysis.PassAnalyses
      let peakWidths = BuildPeakWidths(ipa)
      select peakWidths)
      .SelectMany(data => data);
Run Code Online (Sandbox Code Playgroud)

编辑:变成一个紧凑的小方法:

    public void CreateRunStatistics(Func<IPassAnalysis, IEnumerable<double>> buildMethod, string name)
    {
        var data = runAnalysis.PassAnalyses.SelectMany(buildMethod);
        statistics.Add(StatisticsBase.Calc(name, data));
    }
Run Code Online (Sandbox Code Playgroud)

谢谢!

Ani*_*Ani 5

var runPeakWidths = runAnalysis.PassAnalyses.SelectMany(ipa => BuildPeakWidths(ipa));
Run Code Online (Sandbox Code Playgroud)

如果您愿意,也可以使用它:

var runPeakWidths = runAnalysis.PassAnalyses.SelectMany<Ipa, Pw>(BuildPeakWidths);
Run Code Online (Sandbox Code Playgroud)

其中Ipaipa的类型和PwPeakWidth的类型.

我已经可靠地通知(尚未验证自己)方法组的返回类型推断现在已在编译器中实现,因此这应该在C#4中工作:

var runPeakWidths = runAnalysis.PassAnalyses.SelectMany(BuildPeakWidths);
Run Code Online (Sandbox Code Playgroud)