I have the following table
ItemNumber | FirstId | SecondId | ClientId
1 | 14 | 16 | NULL
2 | 17 | 18 | 1233242323
3 | 14 | 18 | 1233242323
5 | 15 | 12 | NULL
6 | 14 | 8 | 324234252
7 | 19 | 14 | 324234252
8 | 18 | 19 | 324234252
9 | 20 | 18 | 324234252
With following Class
public class ClientObject
{
public int ItemNumber { get; set; }
public int FirstId { get; set; }
public int SecondId { get; set; }
public double ClientId { get; set; }
public ClientObject()
{
}
}
Given the FirstId and ClientId, I want to return SecondId where no matching is found in the FirstId column. Eg: Starting with FirstId as 20 ItemNumber 9), I would like to return 8 (ItemNumber 6).
I am trying to use a recursive function approach but not sure if it is right or if there is a better way to approach this problem.
public ClientObject GetItemRecursive(int initialId, double client)
{
var returnThis = databaseCtxt.TableToUse
.Where(x => x.FirstId == initialId && x.ClientId == client)
.AsEnumerable() // updated from suggestion
.Select(x => GetItemRecursive(x.SecondId, x.ClientId))
.FirstOrDefault();
return returnThis ;
}
I have tried to set this up locally but have not been able to as this is a very very tiny part of large project, so I created the smallest examples here.