是否可以在ForEach lambda表达式中执行此操作?

reports.ForEach(x => new PublishReport(x)
      PublishReport.PublishReports());


我想做的是实例化PublishReport(带有类和方法名的类)类,该类传递一个报告对象,然后调用PublishReport类的PublishReports方法。

我所做的工作是:

reports.ForEach(x => CallPublishReports(x));

private void CallPublishReport(Report report)
{
    PublishReport publishReport = new PublishReport(report);
    publishReport.PublishReports();
}

最佳答案

您要查找的代码如下所示:

reports.ForEach(x => (new PublishReport(x)).PublishReport());


要么

reports.ForEach(x => {
    var report = new PublishReport(x);
    report.PublishReports();
});


您也可以用ForEach替换Select,因为前者仅存在于PLINQ afaik中。

10-07 16:49