LiteDB - Query data

要取得 LiteDB 內的資料,首先需先將 LiteDB 開啟,取得對應的 Collection,調用 Collecion.FindAll 即可取得 Collection 內所有的資料。

1
2
3
4
5
6
7
8
...
using (var db = new LiteDatabase(dbFile))
{
var collection = db.GetCollection<T>(collectionName);
...
var collectionItems = collection.FindAll();
...
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using System;

namespace LiteDB.Demo8
{
class Program
{
static void Main(string[] args)
{
using (var db = new LiteDatabase("Person.db"))
{
var persons = db.GetCollection<Person>("persons");

foreach (var person in persons.FindAll())
{
Console.WriteLine(person.NickName);
}
}
}
}

public class Person
{
public int ID { get; set; }
public String Name { get; set; }
public String NickName { get; set; }
}
}


要找尋滿足特定條件的資料的話,可以調用 Collection.Find 方法,並用 Lambda 帶入要過濾的條件。

1
2
3
4
5
6
7
8
...
using (var db = new LiteDatabase(dbFile))
{
var collection = db.GetCollection<T>(collectionName);
...
var collectionItems = collection.Find(item => filter(item));
...
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using System;

namespace LiteDB.Demo8
{
class Program
{
static void Main(string[] args)
{
using (var db = new LiteDatabase("Person.db"))
{
var persons = db.GetCollection<Person>("persons");

foreach (var person in persons.Find(item => item.ID < 10))
{
Console.WriteLine(person.NickName);
}
}
}
}

public class Person
{
public int ID { get; set; }
public String Name { get; set; }
public String NickName { get; set; }
}
}


如果要過濾的資料只有單筆的話,可以調用 Collection.FindOne 方法。

1
2
3
4
5
6
7
8
...
using (var db = new LiteDatabase(dbFile))
{
var collection = db.GetCollection<T>(collectionName);
...
var collectionItem = collection.FindOne(item => filter(item));
...
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using System;

namespace LiteDB.Demo8
{
class Program
{
static void Main(string[] args)
{
using (var db = new LiteDatabase("Person.db"))
{
var persons = db.GetCollection<Person>("persons");

var larry = persons.FindOne(item => item.ID == 1);
Console.WriteLine(larry.NickName);
}
}
}

public class Person
{
public int ID { get; set; }
public String Name { get; set; }
public String NickName { get; set; }
}
}


要過濾出指定索引資料的話,可帶入要抓取資料的索引值去調用 Collection.FindById。

1
2
3
4
5
6
7
8
...
using (var db = new LiteDatabase(dbFile))
{
var collection = db.GetCollection<T>(collectionName);
...
var collectionItem = collection.FindById(itemIdx);
...
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using System;

namespace LiteDB.Demo8
{
class Program
{
static void Main(string[] args)
{
using (var db = new LiteDatabase("Person.db"))
{
var persons = db.GetCollection<Person>("persons");

var larry = persons.FindById(1);
Console.WriteLine(larry.NickName);
}
}
}

public class Person
{
public int ID { get; set; }
public String Name { get; set; }
public String NickName { get; set; }
}
}