提醒应用c# (Mongodb):如何过滤Mongodb的数据并比较它们,特别是datetime和timespan,并给



大家好,我刚开始编程,我只是想要一些帮助或建议,创建一个简单的c# winforms提醒应用程序

我想创建一个提醒应用程序,它输入日期和时间的提醒与名称和描述。实际上我想用timertick来做这个,但我想先用一个按钮来检查提醒时间和日期,并与系统时间和日期进行比较,但对我来说想出这个工作的算法有点难。最后,如果提醒日期和提醒时间小于或等于系统日期和系统时间给出一个消息框提醒提醒和删除比特定的提醒。我想出了它的大部分,但我被它比较值的逻辑卡住了。

using MongoDB.Driver;
using System;
using System.Linq;
using System.Windows.Forms;
namespace MongoDBTest
{
public partial class Reminder : Form
{
string connectionString = "mongodb://localhost:27017";
public string databaseName = "MongoDB";
public string collectionName = "Reminders";
IMongoCollection<remindermodel> reminderCollection;
public Reminder()
{
InitializeComponent();
timepicker.Format = DateTimePickerFormat.Time;
timepicker.ShowUpDown = true;
datepicker.Format = DateTimePickerFormat.Custom;
datepicker.CustomFormat = "MM/dd/yyyy";
var client = new MongoClient(connectionString);
var db = client.GetDatabase(databaseName);
reminderCollection = db.GetCollection<remindermodel>(collectionName);
}

private void button1_Click(object sender, EventArgs e)
{
DateTime setdate = datepicker.Value; // TRYING TO SET date only here but cudnt
DateTime datetoday = DateTime.Now;
DateTime dt = this.timepicker.Value;

var settime = dt.TimeOfDay;
var set = settime.ToString();
var currenttime = datetoday.TimeOfDay;

if ((datetoday < setdate) && (currenttime < settime))
{
MessageBox.Show("Reminder Set!!");
var remindermodel = new remindermodel
{
remindername = aname.Text,
reminderdate = setdate,
remindertime = TimeSpan.Parse(set),
reminderdescription = reminderdesc.Text,
};
label9.Text = setdate.ToString();
label8.Text = set;
reminderCollection.InsertOneAsync(remindermodel);
}
else
{
MessageBox.Show("Reminder Cannot be set!!");
}

}

private void button2_Click(object sender, EventArgs e)
{
DateTime datenow = DateTime.Now;
var timenow = datenow.TimeOfDay;

var filtertime = Builders<remindermodel>.Filter.Gte(b => b.reminderdate, datenow);
var filterDefinition = Builders<remindermodel>.Filter.Gte(b => b.reminderdate, datenow) & filtertime;
var projection = Builders<remindermodel>.Projection.Exclude("_id").Include("reminderdate").Include("remindertime");
var reminders = reminderCollection.Find(filterDefinition).Project<remindermodel>(projection)    
.FirstOrDefault();
var remindersdatagrid = reminderCollection.Find(filterDefinition).Project<remindermodel>(projection)
.ToList();
datagridview.DataSource = reminders;

if(reminders != null)
{
label7.Text = reminders.reminderdate.ToString();
label8.Text = reminders.remindertime.ToString();
MessageBox.Show("Reminder found");
}
else
{
MessageBox.Show("Sorry No reminders");
}
}

}
}
我的提醒模型是
using System;
namespace MongoDBTest
{
public class remindermodel
{
public string remindername { get; set; }
public DateTime reminderdate { get; set; }
public TimeSpan remindertime { get; set; }
public string reminderdescription { get; set; }
}
}
谁能帮我用逻辑来比较这些时间和日期?因为即使从datetimepicker设置时间,它也会设置像05/09/2022 18 39 PM这样的值。我似乎无法摆脱最后的时间部分,因为我从时间选择器中单独获得时间。

我建议将提醒日期和时间存储为MongoDB中的单个字段。这简化了比较,因为您只需要比较一个字段:

public class remindermodel
{
public string remindername { get; set; }
public DateTime reminderdate { get; set; }
// Not needed: public TimeSpan remindertime { get; set; }
public string reminderdescription { get; set; }
}

然后,当从UI检索日期和时间时,您可以首先从DatePicker获取日期,并从TimePicker添加时间:

private void button1_Click(object sender, EventArgs e)
{
DateTime setdate = datepicker.Value; 
var timeOfDay = this.timepicker.Value.TimeOfDay;
// .Date gets the date part from the DatePicker, 
// Add adds the time that was selected in the TimePicker
setDate = setDate.Date.Add(timeOfDay);
// ... assign setDate to remindermodel.reminderdate later on

过滤提醒时,您只需要比较时间字段:

private void button2_Click(object sender, EventArgs e)
{
DateTime datenow = DateTime.Now;
// Not needed anymore: var filtertime = Builders<remindermodel>.Filter.Gte(b => b.reminderdate, datenow);
// If you want to show reminders of the past, change comparison to Lte (Less than or equal)
var filterDefinition = Builders<remindermodel>.Filter.Gte(b => b.reminderdate, datenow);
// ...

我是这样做的。使用date属性

比较日期
var time_comparison_result = TimeSpan.Compare(settime,currenttime);
var date_comparison_result = DateTime.Compare(setdate.Date,datetoday.Date);
if ((date_comparison_result>0) && 
(time_comparison_result>0))
{
//Set reminder
}
else
{
//MessageBox.Show("Reminder Cannot be set!!");
}

相关内容

  • 没有找到相关文章

最新更新