小编Ton*_*inh的帖子

使用LINQ获取所有可能的不同三元组

我有一个List包含这些值:{1,2,3,4,5,6,7}.而且我希望能够找到三种独特的组合.结果应该是这样的:

{1,2,3}
{1,2,4}
{1,2,5}
{1,2,6}
{1,2,7}
{2,3,4}
{2,3,5}
{2,3,6}
{2,3,7}
{3,4,5}
{3,4,6}
{3,4,7}
{3,4,1}
{4,5,6}
{4,5,7}
{4,5,1}
{4,5,2}
{5,6,7}
{5,6,1}
{5,6,2}
{5,6,3}
Run Code Online (Sandbox Code Playgroud)

我已经有2个for循环可以做到这一点:

for (int first = 0; first < test.Count - 2; first++)
{
  int second = first + 1;
  for (int offset = 1; offset < test.Count; offset++)
  {
    int third = (second + offset)%test.Count;
    if(Math.Abs(first - third) < 2)
      continue;
    List<int> temp = new  List<int>();
    temp .Add(test[first]);
    temp .Add(test[second]);
    temp .Add(test[third]);
    result.Add(temp );
  }
}
Run Code Online (Sandbox Code Playgroud)

但是因为我正在学习LINQ,我想知道是否有更聪明的方法来做到这一点?

c# linq unique

5
推荐指数
1
解决办法
1267
查看次数

标签 统计

c# ×1

linq ×1

unique ×1