4

在使用 LiteCollection 的实例方法 Find() 时,我将谓词作为参数传递给它。这是 Find() 方法:stus.Find(s => s.Birthday.Year <= 1996);,完整代码在这里:

学生.cs:

public class Student
{
    public ObjectId ID { get; set; }
    public string Name { get; set; }
    public int Grade { get; set; }
    public DateTime Birthday { get; set; }
}

程序.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:"一行被打印到控制台。那么这里有什么问题或者我错过了什么吗?感谢您阅读我的问题。

4

1 回答 1

-1

你不能在liteDB中做到这一点

您必须创建一个 DateTime 对象

var d = new DateTime(1996);
var filteredStudents = stus.Find(s => s.Birthday.Year <= d);
于 2020-12-04T15:18:34.447 回答