Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.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
Sql server 根据指向同一表的字段对查询排序_Sql Server_Database_Linq_Tsql - Fatal编程技术网

Sql server 根据指向同一表的字段对查询排序

Sql server 根据指向同一表的字段对查询排序,sql-server,database,linq,tsql,Sql Server,Database,Linq,Tsql,我有一个名为“句子”的表,其中包含以下字段: ID <--- OK NextID <--- FK To ID Text 如果我知道序列的开头是ID=1的记录,那么有没有一种方法可以根据NextID的序列对查询进行排序。与上面的例子一样,预期结果应该是 The quick brown fox jumps over the lazy dog. 我正在寻找一个T-SQL语句,或者以某种方式使用Linq来实现这一点。提前谢谢 试试这个: declare @Your

我有一个名为“句子”的表,其中包含以下字段:

ID         <--- OK
NextID     <--- FK To ID
Text
如果我知道序列的开头是ID=1的记录,那么有没有一种方法可以根据NextID的序列对查询进行排序。与上面的例子一样,预期结果应该是

The quick
brown fox
jumps over
the
lazy dog.
我正在寻找一个T-SQL语句,或者以某种方式使用Linq来实现这一点。提前谢谢

试试这个:

declare @YourTable table (RowID int primary key, NextID int, TextValue varchar(50))

INSERT INTO @YourTable VALUES (1 , 12  ,'The quick')
INSERT INTO @YourTable VALUES (3 , 40  ,'jumps over')
INSERT INTO @YourTable VALUES (5 , null,'lazy dog.')
INSERT INTO @YourTable VALUES (12, 3   ,'brown fox')
INSERT INTO @YourTable VALUES (40, 5   ,'the')

;with cteview as (
SELECT * FROM @YourTable WHERE RowID=1
UNION ALL
SELECT y.* FROM @YourTable y
    INNER JOIN cteview   c ON y.RowID=c.NextID
) 
select * from cteview
OPTION (MAXRECURSION 9999) --go beyond default 100 levels of recursion to 9999 levels
输出:

RowID       NextID      TextValue
----------- ----------- --------------------------------------------------
1           12          The quick
12          3           brown fox
3           40          jumps over
40          5           the
5           NULL        lazy dog.

(5 row(s) affected)
林奇回答:

table.OrderBy(sentence => sentence.NextID);
编辑:我希望这次回答正确:

class Sentence
{
    public int Id;
    public int? NextId;
    public string Text;
    public Sentence(int id, int? nextId, string text)
    {
        this.Id = id;
        this.NextId = nextId;
        this.Text = text;
    }
}

var Sentences = new [] {
    new Sentence(1, 12, "This quick"),
    new Sentence(3, 40, "jumps over"),
    new Sentence(5, null, "lazy dog."),
    new Sentence(12, 3, "brown fox"),
    new Sentence(40, 5, "the"),
};

Func<int?, string> GenerateSentence = null;
GenerateSentence = (id) => id.HasValue? Sentences
    .Where(s => s.Id == id.Value)
    .Select(s => s.Text + " " + GenerateSentence(s.NextId))
    .Single() : string.Empty;

Console.WriteLine(GenerateSentence(1));
类语句
{
公共int Id;
公共int?NextId;
公共字符串文本;
公共语句(int-id,int-nextId,字符串文本)
{
这个.Id=Id;
this.NextId=NextId;
this.Text=文本;
}
}
var语句=新[]{
新的句子(1,12,“这个快速”),
新的句子(3,40,“跳过”),
新句子(5,空,“懒狗”。),
新的句子(12,3,“棕色狐狸”),
新的一句(第40、5节,“本条”),
};
Func GenerateSentence=null;
生成内容=(id)=>id.HasValue?句子
.其中(s=>s.Id==Id.Value)
.Select(s=>s.Text+“”+生成内容(s.NextId))
.Single():string.Empty;
控制台写入线(生成内容(1));

如果您使用的是LINQ to SQL/Entities,那么生成的
语句
类应该具有您提到的所有属性,以及从外键对下一个句子(我们称之为NextContent)的实体引用

然后你可以做:

Sentence s = Sentences.First();
StringBuilder sb = new StringBuilder();
do { sb.Append(s.Text); s = s.NextSentence; } while (s != null);

sb.ToString()
将得到您的答案

sql链接列表。。。有趣!如果任何使用oracle的人想做同样的事情,我会提到oracle有一个“Connect By”子句,它可以非常简单地解决这个问题,也可以用于更复杂的树结构。这不会给你一个。。。懒狗。brown fox快速跳转???使用这样的应用程序端解决方案,您必须返回整个表,然后将所有id+NextID重新组合在一起,否则您将不得不使用SELECT for each(为每个id+NextID)点击dbsegment@KM... 这就是我试图避免的。每次命中数据库。这将最多使用100个递归,然后您必须配置SQL Server以允许更高的递归深度。您不必更改服务器默认值100。如果在select*from cteview之后添加选项(MAXRECURSION n),其中n可以在0到32767个递归级别之间(零没有限制),这看起来很整洁,但我想知道是否可以将其转换为LINQ语句。+1非常聪明。我不知道这样的递归表达式在linq to sql中可能是公共表表达式(CTE):
Sentence s = Sentences.First();
StringBuilder sb = new StringBuilder();
do { sb.Append(s.Text); s = s.NextSentence; } while (s != null);