Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.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# C每一个问题_C#_Foreach - Fatal编程技术网

C# C每一个问题

C# C每一个问题,c#,foreach,C#,Foreach,C在重构方面对以下代码有什么贡献吗 我想能够用一个调用来代替两个foreach调用,就像每个x,x对可能的调用一样,可以这样做吗 有Zip方法有Zip方法没有什么比Python的itertools.product更简洁了,但是你可以像博客作者那样使用Linq。没有什么比Python的itertools.product更简洁了,但是你可以像博客作者那样使用Linq。效率不高: var permutations = sequence.Images.SelectMany (image1 => s

C在重构方面对以下代码有什么贡献吗

我想能够用一个调用来代替两个foreach调用,就像每个x,x对可能的调用一样,可以这样做吗


有Zip方法

有Zip方法

没有什么比Python的itertools.product更简洁了,但是你可以像博客作者那样使用Linq。

没有什么比Python的itertools.product更简洁了,但是你可以像博客作者那样使用Linq。

效率不高:

var permutations = sequence.Images.SelectMany (image1 => sequence.Images.Where (image2 => image1 != image2).Select(image2 => new ImagePair (image1, image2)));
效率不高:

var permutations = sequence.Images.SelectMany (image1 => sequence.Images.Where (image2 => image1 != image2).Select(image2 => new ImagePair (image1, image2)));

我倾向于把它分成两个阶段。首先是生成所需对序列的查询,其次是序列上的foreach:

var pairs = from image1 in sequence.Images
            from image2 in sequence.Images
            where image1 != image2
            select new ImagePair(image1, image2);

foreach(var pair in pairs)
    metric.SetImageMetric(pair, 1.0);

我倾向于把它分成两个阶段。首先是生成所需对序列的查询,其次是序列上的foreach:

var pairs = from image1 in sequence.Images
            from image2 in sequence.Images
            where image1 != image2
            select new ImagePair(image1, image2);

foreach(var pair in pairs)
    metric.SetImageMetric(pair, 1.0);

我觉得这个问题可以由LINQ来回答,但我对此一无所知。我想您需要一个调用来执行完整的笛卡尔积。您实际上不是在寻找图像1!=Image2测试要包含在这个单一调用中,还是您?我感觉这个问题可以由LINQ回答,但我对此一无所知。我想您需要一个调用来执行完整的笛卡尔积。您实际上不是在寻找图像1!=Image2测试是否包含在这个调用中?Zip方法没有给出两个列表之间所有可能的排列。Zip在这里不起作用,因为@Yippie希望所有可能的图像配对,除非两个图像相同。原始帖子用Zip标记了这个,我认为这应该是产品。我重新标记了.Zip方法并没有给出两个列表之间所有可能的排列。Zip在这里不起作用,因为@Yippie需要所有可能的图像配对,除非两个图像是相同的。原始帖子用Zip标记了这个,我认为这应该是产品。我重新标记了,我同意。然而,这个问题只需要一个电话。我用Eric Lippert在LINQ中的笛卡尔产品实现的链接来更新答案,因为这应该是最简单、最漂亮的方法。我同意。然而,这个问题只需要一个电话。我用Eric Lippert在LINQ中的笛卡尔产品实现的链接来更新答案,因为这应该是最简单、最漂亮的方法。