如何将下面的foreach循环转换为linq代码格式? [英] How to convert the following foreach loop to linq code format?

查看:138
本文介绍了如何将下面的foreach循环转换为linq代码格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

  foreach(var testView in projectDataCandidate.ViewMaps 
.Where(vm =>!vm .IsNotTestLane)
.Select(this.GenerateTestView))
{
this.SwimLaneViews.Add(testView);
testView.ItemsOrphaned + = OnItemsOrphaned;



$ b $ p $如何在LINQ代码格式中编写上面的foreach循环?

解决方案

  projectDataCandidate.ViewMaps 
.Where(vm => !vm.IsNotTestLane)
.Select(this.GenerateTestView)
.ToList()
.ForEach(testView =>
{
this.SwimLaneViews.Add( testView);
testView.ItemsOrphaned + = OnItemsOrphaned;
});


Code:

foreach (var testView in projectDataCandidate.ViewMaps
                                             .Where(vm => !vm.IsNotTestLane)
                                             .Select(this.GenerateTestView))
{
      this.SwimLaneViews.Add(testView);
      testView.ItemsOrphaned += OnItemsOrphaned;
}

How to write the above foreach loop in linq code format ? Thanks.

解决方案

projectDataCandidate.ViewMaps
    .Where(vm => !vm.IsNotTestLane)
    .Select(this.GenerateTestView)
    .ToList()
    .ForEach(testView =>
    {
        this.SwimLaneViews.Add(testView);
        testView.ItemsOrphaned += OnItemsOrphaned;
    });

这篇关于如何将下面的foreach循环转换为linq代码格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆