如何在c#中查询窗体之间的日期 [英] how to query for date in between for window forms in c#

查看:87
本文介绍了如何在c#中查询窗体之间的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发win form application。问题是我想从datetimepicker1.value找到数据到

datetimepicker2.value这里为ex。



如果我想获取数据'3/19/2014'和'3/20/2014'之间的日期我在2014年3月19日获得了所有增加的值,但未在3/20/2014上添加值

i am developing win form application. problem is i want to find data from datetimepicker1.value to
datetimepicker2.value here for ex.

if i want to get data for Date between '3/19/2014' and '3/20/2014' i get all the values added up on 3/19/2014 but doesn't get values added on 3/20/2014

推荐答案

可能的问题之一是您从DateTimePicker组件获取完整的DateTime值。在这种情况下,DateTime值的Time部分可能很重要。



假设您为变量分配值以进行过滤:

One of the possible problems is that you are getting full DateTime values from DateTimePicker components. In this case Time part of DateTime value can be significant.

Asuming that you are assigning values to variables for filtering purposes:
var startDate = datetimepicker1.Value;
var endDate = datetimepicker2.Value;





可能是你将获得价值:

startDate = 3/19/2014 09:13:35

endDate = 3/20/2014 09:13:35


(根据DateTimePicker组件初始化时间时间部分的时间可能不同)。



解决方案是仅提取日期部分并忽略时间部分:



probably you will get values:
startDate = 3/19/2014 09:13:35
endDate = 3/20/2014 09:13:35

(according to time when DateTimePicker components where initialized Time part can be different).

Solution is to extract only Date part and ignore Time part:

var startDate = datetimepicker1.Value.Date;
var endDate = datetimepicker2.Value.Date;





然后你会得到这样的价值:

startDate = 3/19/2014 00:00:00

endDate = 3/20/2014 00:00:00



下一步是以适当的方式过滤数据。例如,当您使用EF和LINQ从DB获取一些数据时,请尝试以下方法:





then you will get values like this:
startDate = 3/19/2014 00:00:00
endDate = 3/20/2014 00:00:00

Next step is to filter your data in proper way. For example when you are using EF and LINQ to get some data from DB try this:

var invoices = from inv in db.Invoices
               where inv.InvoiceDate.Date >= startDate
                  && inv.InvoiceDate.Date <= endDate
               select x;





现在你应该在选定的时间内获得所有价值。



[更新]

如果您使用数据库(例如MS SQL Server)进行存储,则可以选择日期字段类型。它将自动仅存储日期部分。当你不需要存储时间部分时它很有用。



希望它可以帮助你:)



Now you should get all values in selected period.

[Update]
If you are using database (such as MS SQL Server) for storage purposes then you can choose Date field type. It will automatically store only Date part. It's helpful when you don't need to store Time part.

Hope it helps you :)


using(DBDataContext db = new DBDataContext())

{

var obj = db.YOURTABL.where(w => w.yourDate< = ToDate && w.yourDate< = FromDate)。ToList();

}
using(DBDataContext db = new DBDataContext())
{
var obj = db.YOURTABL.where(w=> w.yourDate <= ToDate && w.yourDate <= FromDate).ToList();
}


这篇关于如何在c#中查询窗体之间的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆