【发布时间】:2025-11-30 20:05:02
【问题描述】:
我正在尝试从数据库中获取所有引用的值,直到最后一个值为空。
示例:如果我必须从下表中搜索 ID - 1:
╔════╦════════╗
║ ID ║ RefID ║
╠════╬════════╣
║ 1 ║ 2 ║
║ 1 ║ 3 ║
║ 2 ║ 4 ║
║ 3 ║ Null ║
║ 4 ║ 6 ║
║ 5 ║ Null ║
║ 6 ║ Null ║
╚════╩════════╝
结果: 2 3 4 6
我试过了:
using (DbContext Db = new DbContext())
{
string ID = "1";
List<string> ids = new List<string>();
while (true)
{
List<string> t = Db.Test.Where(x => x.ID == ID).Select(x => x.RefID).ToList();
foreach (string item in t)
{
if (item != null)
{
ID = item;
ids.Add(item);
}
else
{
break;
}
}
}
}
感谢任何帮助。
【问题讨论】:
-
表看起来像父/子表。您可以添加一个新列来存储hierarchy path
标签: sql-server entity-framework linq