这是我的对象模型:
class PhoneBookCategory
{
public Guid Id { get; set; }
public List<PhoneBook> PhoneBooks { get; set; }
}
class PhoneBook
{
public Guid Id { get; set; }
}
我正在寻找的是“所有具有 ID 位于列表中的电话簿的 PhoneBookCategories”。或者,在 Linq 语法中
List<Guid> myList = [Guid.NewGuid(), Guid.NewGuid()];
GetCollection<PhoneBookCategory>()
.Find(u => u.PhoneBooks.Any(x => myList.Contains(x.Id));
LiteDB 的 Linq 解析器无法处理这个问题,所以我尝试使用查询语法并最终得到这里:
var bsArray = new BsonArray();
myList.Foreach(u => bsArray.Add(u));
GetCollection<PhoneBookCategory>().Query()
.Where(Query.In("$.PhoneBooks[*].Id ALL", bsArray))).ToList();
但让我感动的是这个
GetCollection<PhoneBookCategory>()
.Find(u => u.PhoneBooks == null || u.PhoneBooks.Count == 0)
相反。如果我用
ALL
替换 ANY
,我最终不会得到任何结果。
你可以这样尝试。
我创建了一个简单的存储库来集中功能和一个测试方法来测试结果。
public class PhoneBookRepository
{
private ConnectionString _connectionString;
private const string _categories_Collection = "categories";
private const string _phoneBooks_Collection = "phoneBooks";
public PhoneBookRepository(string fileName)
{
_connectionString = new ConnectionString(fileName);
}
public List<PhoneBookCategory> GetPhoneBookCategoriesFromPhoneBook(PhoneBook phoneBook)
{
using (var db = new LiteDatabase(_connectionString))
{
var _categoryCollection = db.GetCollection<PhoneBookCategory>(_categories_Collection);
var categories = _categoryCollection.Find(c => c.PhoneBooks.Select(pb => pb.Id).Contains(phoneBook.Id)).ToList();
return categories;
}
}
public void AddPhonebook(PhoneBook pb)
{
using (var db = new LiteDatabase(_connectionString))
{
db.BeginTrans();
var _collection = db.GetCollection<PhoneBook>(_phoneBooks_Collection);
_collection.Insert(pb);
db.Commit();
}
}
public void AddPhoneBookCategories(PhoneBookCategory pbc)
{
using (var db = new LiteDatabase(_connectionString))
{
db.BeginTrans();
var _collection = db.GetCollection<PhoneBookCategory>(_categories_Collection);
_collection.Insert(pbc);
db.Commit();
}
}
}
这是带有测试方法的测试类:
[TestClass]
public class LitedbPhoneBookTest
{
[TestMethod]
public void CanGetPhoneBookCategories()
{
var pb1 = new PhoneBook()
{
Id = Guid.NewGuid(),
};
var pb2 = new PhoneBook()
{
Id = Guid.NewGuid(),
};
var pbc1 = new PhoneBookCategory() {
Id = Guid.NewGuid(),
PhoneBooks = new List<PhoneBook>()
{ pb1 }
};
var pbc2 = new PhoneBookCategory()
{
Id = Guid.NewGuid(),
PhoneBooks = new List<PhoneBook>()
{ pb2 }
};
var pbc3 = new PhoneBookCategory()
{
Id = Guid.NewGuid(),
PhoneBooks = new List<PhoneBook>()
{ pb1,pb2 }
};
var answerCodeRepository = new PhoneBookRepository("phoneBooks.db");
answerCodeRepository.AddPhonebook(pb1);
answerCodeRepository.AddPhonebook(pb2);
answerCodeRepository.AddPhoneBookCategories(pbc1);
answerCodeRepository.AddPhoneBookCategories(pbc2);
answerCodeRepository.AddPhoneBookCategories(pbc3);
var pbc1_result = answerCodeRepository.GetPhoneBookCategoriesFromPhoneBook(pb1);
var pbc2_result = answerCodeRepository.GetPhoneBookCategoriesFromPhoneBook(pb2);
Assert.AreEqual(2,pbc1_result.Count);
Assert.AreEqual(2,pbc2_result.Count);
}
}
我希望这有帮助,请告诉我。