0
我有一個表只包含一列 - FolderPath。我可以很容易地從該表中使用這樣的如何獲取SQLite表中的項目索引?
var item = from c in db.Table<Profile>() where c.FolderPath == "C:\\" select c;
但是如何一個代碼來獲得一個表中的號碼這個項目(編號一排)的得到一個項目?是否可以不添加額外的列像「Id」?
我有一個表只包含一列 - FolderPath。我可以很容易地從該表中使用這樣的如何獲取SQLite表中的項目索引?
var item = from c in db.Table<Profile>() where c.FolderPath == "C:\\" select c;
但是如何一個代碼來獲得一個表中的號碼這個項目(編號一排)的得到一個項目?是否可以不添加額外的列像「Id」?
您可以這樣做:,但您不能確定index
,因爲Select
沒有orderby可能會導致不確定的順序。
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);
要確保你可以使用排序依據,如順序:
var itemWithIndex = db.Table<Profile>()
.OrderBy(r=> r.FolderPath)
.Select((r, i) => new {Value =r, Index = i})
.FirstOrDefault(r=> r.Value.FolderPath == "C:\\");
我不知道你爲什麼要避免一個額外的列在表中指定的ID
。這可能是一個更好的選擇,因爲即使在添加或更新記錄的情況下,它也會保持不變。