0

I have a MS SQL server with table who is linked on itself

| Id| PreviousId|  Decription |
|---|:---------:|------------:|
| 1 | null      | Blah        |
| 2 | 1         | Blah        |
| 3 | 2         | Blah        |

And I need to get list of all records starting from Id=3.

My list should look like:

public class Record{
    public int Id {get;set;}
    public int? PrevId {get;set;}
    public string Desc {get;set;}
}
List<Record> records= new List<Record>();

/*Code to get all records*/

//Result
record[0] = Record(){Id=3,PrevId=2,Descr="Blah"} 
record[1] = Record(){Id=2,PrevId=1,Descr="Blah"} 
record[2] = Record(){Id=1,PrevId=null,Descr="Blah"} 

Thank you!

EDIT1: Sorry guys, but i didn't mentioned that ID are not in order. And there can be situation when, for example, record with ID=17 link to previous record with id =12

SBarkovskis
  • 124
  • 2
  • 11

3 Answers3

0

Assuming your DbContext is something like:

public class MyDbContext : DbContext
{
   public MyDbContext(string connectionString) : base(connectionString) {}
   public DbSet<Record> Records {get; set;}
}

The code to retrieve records from table is:

var db = new MyDbContext(connectionString);
var records = db.Records.ToList();

or, if you want to use LINQ syntax:

var db = new MyDbContext(connectionString);
var recordsQuery = from record in db.Records
                   select record;
var records = recordsQuery.ToList();

Note: since question is pretty basic, I suggest you to have a look at EntityFramework documentation or courses (Julie Lerman courses and books are great, IMO).

Claudio Valerio
  • 2,302
  • 14
  • 24
0

Assuimng your table is named Record, then in your model (context class) you should have property represetning table calles Record or Records (if you set pluralising names in your model). So, getting records from table is done via that property on which you can use normal LINQ methods, such as Where:

var result = dbContext.Records.Where(r => r.Id >= 3).ToList();

ToList() will cause query to execute.

EDIT: to filter based on PreviousId, then use:

var result = dbContext.Records.Where(r => r.PreviousId == 3).ToList();
Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
0

I edited my answer to meet the requirements in getting the heirarchy.

Put this in your main function:

        var idList = GetList();

        //The complete heirarchy list
        var ancestorList = new List<Record>();

        //startId will determine the beginning Id of your query
        int? startId = 3;

        while (true)
        {

            if (startId != null)
            {
                ancestorList.Add(GetList()
                  .Where(m => m.Id == startId)
                  .FirstOrDefault());
            }
            else
            {
                break;
            }

            startId = GetPreviousId((int)startId);
        }

        //this is just for checking the list of ids, this is unnecessary
        foreach (var item in ancestorList)
        {
            Console.WriteLine(item.Id);
        }

        Console.ReadLine();

Retrieving all the data:

public List<Record> GetList(){

     var records = new List<Record>();

     using(YourDBContext db = new YourDBContext())
     {
        records = db.records
         .Select(s=> new Record()
         {
           Id = s.Id,
           PrevId = s.PreviousId,
           Desc = s.Description,
         }).ToList();
     }

     return records;
}

Retrieving the previous Id:

    public static int? GetPreviousId(int startId)
    {
        return GetList()
               .Where(m => m.Id == startId)
               .Select(m => m.PrevId) 
               .FirstOrDefault();
    }
javachipper
  • 519
  • 4
  • 15