I am trying to get all referenced values from database until last value is null.
Example: If i have to search ID - 1 from the below table :
╔════╦════════╗
║ ID ║ RefID ║
╠════╬════════╣
║ 1 ║ 2 ║
║ 1 ║ 3 ║
║ 2 ║ 4 ║
║ 3 ║ Null ║
║ 4 ║ 6 ║
║ 5 ║ Null ║
║ 6 ║ Null ║
╚════╩════════╝
Result : 2 3 4 6
I have tried:
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;
}
}
}
}
Any help is appreciated.