문제

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"?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top