Question

I have a table which contains only one column - FolderPath. I can easily get an item from this table using a code like that

var item = from c in db.Table<Profile>() where c.FolderPath == "C:\\" select c;

But how to get a number of this item (number of a row) in a table? Is it possible without adding additional column like "Id"?

Was it helpful?

Solution

You can do something like:, but you can't be sure about the index because Select without an orderby can result in an indeterminate order.

var itemWithIndex = db.Table<Profile>()
                      .Select((r, i) => new {Value =r, Index = i})
                      .FirstOrDefault(r=> r.Value.FolderPath == "C:\\");


if(itemWithIndex != null)
   Console.WriteLine(itemWithIndex.Index); 

To be sure about the order you can use OrderBy like:

var itemWithIndex = db.Table<Profile>()
                      .OrderBy(r=> r.FolderPath)
                      .Select((r, i) => new {Value =r, Index = i})
                      .FirstOrDefault(r=> r.Value.FolderPath == "C:\\");

I am not sure why you are avoiding an extra column to specify the ID in the table. Probably its a better option, since this will remain same even in case of addition or updation of records.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top