4

When using LiteCollection's instance method Find(), I passed a predicate into it as a parameter. This is the Find() method: stus.Find(s => s.Birthday.Year <= 1996);, and full code here:

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);
        }
    }
}

But filteredStudents doesn't contain any student, only "Student who has birth year before 1996:" line was printed to the console. So is anything here wrong or I missed something here? Thank you for reading my question.

namhnz
  • 75
  • 1
  • 10

1 Answers1

-1

You can not do that in liteDB

You must create a DateTime Object

var d = new DateTime(1996);
var filteredStudents = stus.Find(s => s.Birthday.Year <= d);