用循环转换此linq表达式

rcj*_*rcj 1 c# linq loops for-loop

我正在尝试调试开发人员编写的代码,LINQ表达式使任务变得痛苦.我不知道如何调试复杂的LINQ表达式,所以任何人都可以告诉我没有它们的等效代码是什么?

instanceIdList.AddRange(
  strname.Instances
    .Where(z => instancehealthList.Find(y => y.InstanceId == z.InstanceId 
                                          && y.State == "InService") != null)
    .Select(x => x.InstanceId)
    .ToList()
  .Select(instanceid => new ServerObj(servertype, instanceid))
);
Run Code Online (Sandbox Code Playgroud)

写得好吗?一般来说,这种LINQ是鼓励还是不赞成?

Jam*_*son 5

使用循环重构查询看起来像这样:

var serverObjList = new List<ServerObj>();
foreach (var inst in strname.Instances)
{
    foreach (var health in instancehealthList)
    {
        if (inst.InstanceID == health.InstanceID && health.State == "InService") 
        {
            serverObjList.Add(new ServerObj(servertype, health.InstanceID));
            break;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)