c# - LiteDB Find() 与 DateTime.Year 比较没有任何结果

标签 c# nosql litedb

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

最佳答案

您不能在 中这样做liteDB
您必须创建一个 DateTime 对象

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

关于c# - LiteDB Find() 与 DateTime.Year 比较没有任何结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55694031/

相关文章:

c# - 如何从 xaml 访问 UserControl 中的按钮?

c# - 当我将它设置为详细信息时,如何将图像添加到 ListView?

c# - 从 LiteDB 获取数据

c# - 如何让 WPF 窗口根据内容自动调整大小

java - 使用默认方法与抽象类的接口(interface),动机是什么?

java - 在java中使用Jedis在redis中搜索

mongodb - 有什么理由不使用 OrientDB 吗?

c# - 无法加载反序列化所需的类型

c# - Xamarin LiteDB UnauthorisedAccessException

c# - LiteDB:字段 'Null' 上的 BSON 数据类型 '_id' 无效