使用LiteCollection的实例方法Find()时,我将谓词作为参数传递给它。这是Find()方法:stus.Find(s => s.Birthday.Year <= 1996);
,完整代码在这里:
Student.cs:
public class Student
{
public ObjectId ID { get; set; }
public string Name { get; set; }
public int Grade { get; set; }
public DateTime Birthday { get; set; }
}
Program.cs:
class Program
{
static void Main(string[] args)
{
using (var db = new LiteDatabase("test.db"))
{
LiteCollection<Student> stus = db.GetCollection<Student>("students");
List<Student> studentList = new List<Student>()
{
new Student() {Name = "Nguyen Hoang Nguyen", Birthday = new DateTime(1997, 6, 3), Grade = 8},
new Student() {Name = "Nguyen Anh Tuan", Birthday = new DateTime(1997, 7, 12), Grade = 8},
new Student() {Name = "Pham Van Hung", Birthday = new DateTime(1996, 3, 26), Grade = 9}
};
stus.Insert(studentList);
var filteredStudents = stus.Find(s => s.Birthday.Year <= 1996);
Console.WriteLine("Student who has birth year before 1996:");
foreach (Student filteredStudent in filteredStudents)
{
Console.WriteLine($"- {filteredStudent.Name}, grade: {filteredStudent.Grade}");
}
Console.ReadKey();
stus.Delete(_ => true);
}
}
}
但是filteredStudents
不包含任何学生,只有"Student who has birth year before 1996:"
行被打印到控制台。那么这里有什么问题吗,或者我错过了这里的东西?谢谢您阅读我的问题。
答案 0 :(得分:0)
您不能在 liteDB
中执行此操作您必须创建一个DateTime对象
var d = new DateTime(1996);
var filteredStudents = stus.Find(s => s.Birthday.Year <= d);