gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 21:04:07 28 4
gpt4 key购买 nike

在使用 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com