Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将以下foreach循环转换为linq代码格式?_C#_.net_Linq_C# 4.0_Foreach - Fatal编程技术网

C# 如何将以下foreach循环转换为linq代码格式?

C# 如何将以下foreach循环转换为linq代码格式?,c#,.net,linq,c#-4.0,foreach,C#,.net,Linq,C# 4.0,Foreach,代码: 如何以linq代码格式编写上述foreach循环?谢谢。试试看 foreach (var testView in projectDataCandidate.ViewMaps .Where(vm => !vm.IsNotTestLane) .Select(this.GenerateTestView)) {

代码:

如何以linq代码格式编写上述foreach循环?谢谢。

试试看

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

我看到的唯一方法是使用ToList强制执行,然后使用ForEach函数

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

正如其他人已经指出的,您已经在使用LINQ进行查询,LINQ用于查询而不是命令

组织你的逻辑 我认为您正在努力提高代码的可读性。为此,我建议您将查询和命令提取到不同的类成员中:

var objects = projectDataCandidate.ViewMaps
    .Where(vm => !vm.IsNotTestLane)
    .Select(this.GenerateTestView)
    .ToList();

objects.ForEach(a => SwimLaneViews.Add(a));
objects.ForEach(a => a.ItemsOrphaned += OnItemsOrphaned);
使循环成为声明性的 如果您仍然不喜欢foreach的外观,可以这样做,我认为这更容易阅读:

private void ProcessTestViews()
{
    foreach (var testView in TestViews)
        Process(testView);
}
删除冗余的
ToList
如果您不想将结果转换为
列表
,因为它不是真正需要的,那么您可以编写一个扩展方法,为
IEnumerable
提供
ForEach


我会考虑不做重写。要阅读的代码很清楚,
foreach
循环的出现表明了这样一个事实,即你实际上在做一些“非关键的”事情,即你实际上在修改你正在处理的对象,如果你使用
foreach
扩展方法,这些事情就会变得模糊。@Damien_不相信者我100%同意。重写它会模糊其意图。我还建议这样做,尤其是“更难理解,更难调试,并引入闭包语义,从而以微妙的方式潜在地改变对象的生命周期。”部分。
。ForEach
未列在
智能感知
中。您还假设此代码不在PCL或Windows应用商店应用程序中,其中,
ForEach
不受支持。我认为这实际上并不编译;我不认为
ForEach
方法适用于
IEnumerable
。我认为使用
ForEach
循环对结果进行两次迭代比一次更麻烦。是的,我同意,mMctadir的语法是最好的。我只是想概述一下,在使用foreach之前,您需要使用toList并强制执行。
private IEnumerable<ViewMap> TestViews
{
    get
    {
        return projectDataCandidate.ViewMaps
            .Where(vm => !vm.IsNotTestLane)
            .Select(this.GenerateTestView);
    }
}

private void Process(ViewMap testView)
{
    this.SwimLaneViews.Add(testView);
    testView.ItemsOrphaned += OnItemsOrphaned;
}
private void ProcessTestViews()
{
    foreach (var testView in TestViews)
        Process(testView);
}
private void ProcessTestViews()
{
    TestViews.ToList().ForEach(Process);
}
public static class IEnumerableExtensions
{
    public static void ForEach<T>(this IEnumerable<T> elements, Action<T> action)
    {
        foreach (var element in elements)
            action(element);
    }
}
private void ProcessTestViews()
{
    TestViews.ForEach(Process);
}