重复LINQ查询

时间:2010-09-30 15:49:44

标签: c# linq

鉴于我目前的扩展方法:

public static List<char> rotate(this List<char> currentList, int periodes) {
    if (periodes != 1) {
        int x = currentList.Count() - 1;
        return rotate(currentList.Skip(x).
             Concat(currentList.Take(x)).ToList<char>(), periodes - 1);
    }
    return currentList;
}

原始状态:

ring = new List<char>() { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' };

ring.rotate(10);

的当前结果
J A B C D E F G H I
I J A B C D E F G H
H I J A B C D E F G
G H I J A B C D E F
F G H I J A B C D E      Recursive Steps
E F G H I J A B C D
D E F G H I J A B C
C D E F G H I J A B
B C D E F G H I J A

A B C D E F G H I J      Result

有没有办法摆脱这个while循环以及将重复集成到LINQ查询中的任何机会?

最佳
亨利克

3 个答案:

答案 0 :(得分:3)

这应该与原始解决方案相同,并补偿-1问题:

public static List<char> rotate2(this List<char> currentList, int periodes)
{
    int start = (currentList.Count - periodes) + 1;
    return currentList.Skip(start).Concat(currentList.Take(start)).ToList();
}

修改

我把它放到了一个控制台应用程序中,结果看起来和我一样

class Program
{
    static void Main(string[] args)
    {

        List<char> s = "ABCDEFGHIJ".ToList();

        for (int x = 0; x < 10; x++)
        {
            s.rotate(x+ 1).ForEach(Console.Write);
            Console.WriteLine();
        }
        Console.WriteLine();
        for (int x = 0; x < 10; x++)
        {
            s.rotate2(x + 1).ForEach(Console.Write);
            Console.WriteLine();
        }

        Console.ReadLine();
    }
}

static class so
{
    public static List<char> rotate(this List<char> currentList, int periodes)
    {
        while (periodes != 1)
        {
            int x = currentList.Count() - 1;
            return rotate(currentList.Skip(x).
                    Concat(currentList.Take(x)).ToList<char>(), periodes - 1);
        }
        return currentList;
    }

    public static List<char> rotate2(this List<char> currentList, int periodes)
    {
        int start = (currentList.Count - periodes) + 1;
        return currentList.Skip(start).Concat(currentList.Take(start)).ToList();
    }
}

答案 1 :(得分:3)

跳过i并结束i

public static class Ex
{
    public static List<char> Rotate(this List<char> c, int i)
    {
        i %= c.Count;
        return c.Skip(i).Concat(c.Take(i)).ToList();
    }
}

class Program
{
    static void Main()
    {
        List<char> chars = new List<char>();

        for (int i = 65; i < 75; ++i)
        {
            chars.Add((char)i);
        }

        var r1 = chars.Rotate(10); // A B C D E F G H I J
        var r2 = chars.Rotate(1); // B C D E F G H I J A
        var r3 = chars.Rotate(101); // B C D E F G H I J A
        var r4 = chars.Rotate(102); // C D E F G H I J A B

        Console.ReadLine();
    }
}

答案 2 :(得分:1)

要完全使用Linq编写控制台程序,请执行以下操作:

class Program
{
    static void Main(string[] args)
    {
        var ring = Enumerable.Range(97, 10).Select(x => (char)x).ToList();

        Console.WriteLine(string.Join("\n", Enumerable.Range(1, 10).Select(x => new string(ring.rotate(x).ToArray()))));

        Console.ReadLine();
    }
}