如何使用此模式将此sql语句转换为linq

时间:2015-02-12 06:50:21

标签: sql sql-server linq sql-server-2008 linq-to-sql

如何将此sql语句转换为linq。

declare @S nvarchar(100) = 'www.abc.com/a/b/c/d/e/f/g/h/i'

while PATINDEX('%[/]%' , @S) > 0 BEGIN
    SET @S = LEFT (@S,LEN(@S) - PATINDEX('%[/]%' , REVERSE(@S)))
SELECT @S
END

1 个答案:

答案 0 :(得分:2)

这应该这样做:

StringBuilder sb = new StringBuilder();
string url = "www.abc.com/a/b/c/d/e/f/g/h/i";

var q = url.Split('/').Select(s => sb.Append((sb.Length > 0 ? "/" : "") + s).ToString());

foreach(string output in q.Reverse())
    Console.WriteLine(output);

<强>输出:

www.abc.com/a/b/c/d/e/f/g/h/i
www.abc.com/a/b/c/d/e/f/g/h
www.abc.com/a/b/c/d/e/f/g
www.abc.com/a/b/c/d/e/f
www.abc.com/a/b/c/d/e
www.abc.com/a/b/c/d
www.abc.com/a/b/c
www.abc.com/a/b
www.abc.com/a
www.abc.com