简体   繁体   English

如何使用实体框架按日期搜索?

[英]How to search by date using Entity Framework?

I am trying to convert a date to a string so I can search on the value entered.我正在尝试将日期转换为字符串,以便可以搜索输入的值。 I am using a lamda expression and DateTime.ParseExact as I just want to use the short date entered.我正在使用 lamda 表达式和 DateTime.ParseExact,因为我只想使用输入的短日期。

This is my connection to the database:这是我与数据库的连接:

var devices = db.Devices
            .Include(d => d.DeviceType)
            .Include(d => d.ManufacturerModel)
            .Include(d => d.ManufacturerModel.Manufacturer);

and my search和我的搜索

if (!String.IsNullOrEmpty(searchString5))
{
    devices = devices.Where(s => DateTime.ParseExact(s.DateReceived,'dd/MM/yyyy');
}

You cannot compare two dates easily, because you still need to compare hours, minutes and seconds.您不能轻易比较两个日期,因为您仍然需要比较小时、分钟和秒。

Instead, you want to let user to choose ranges - From Date and To Date.相反,您希望让用户选择范围 - 从日期和到日期。

For example,例如,

var query = db.Devices
            .Include(d => d.DeviceType)
            .Include(d => d.ManufacturerModel)
            .Include(d => d.ManufacturerModel.Manufacturer);

string fromDate = "1/15/2016", toDate = "1/30/2016";
    DateTime fromDateTime, toDateTime;

if(!DateTime.TryParse(fromDate, out fromDateTime))
{
    // Make fromDateTime to Start of Day - 1/15/2016 12:00:00 AM
    fromDateTime = fromDateTime.Date;
    query = query.Where(x => x.Date >= fromDateTime);
}

if (!DateTime.TryParse(toDate, out toDateTime))
{
    // Make toDateTime to End of day - 1/30/2016 11:59:59 PM
    toDateTime = toDateTime.Date.AddDays(1).AddTicks(-1);
    query = query.Where(x => x.Date <= toDateTime);
}

var result = query.ToList();

Don't use strings.不要使用字符串。 Let the framework do the heavy lifting for you.让框架为您完成繁重的工作。

DateTime dt;
if (DateTime.TryParse(searchString5, out dt)) {
    qry = qry.Where(s => s.Fecha.Date == dt.Date);
} else {
    throw new Exception("Bad input");
}

The generated SQL (in my case) is as follows, which is quite good.生成的SQL(以我为例)如下,相当不错。

-- Region Parameters
DECLARE @p0 DateTime = '2016-03-19 00:00:00.000'
-- EndRegion
SELECT 
   ...
FROM 
   ...
WHERE CONVERT(DATE, [t0].[Fecha]) = @p0

Here's a tip: use LINQPad to write and analyze your queries.这里有一个提示:使用LINQPad编写和分析您的查询。 It's free (and the paid version is cheap but it's absurdly powerful and useful.)它是免费的(付费版本很便宜,但功能强大且有用。)

Searching dates with entity can be rough I converted my string into a DateTime format then searched each part of the date to pull the date that was needed I'm not sure if this is the direction you wanted to go but this worked for me使用实体搜索日期可能很粗糙我将字符串转换为 DateTime 格式,然后搜索日期的每个部分以提取所需的日期我不确定这是否是您想要的方向 go 但这对我有用

                    DateTime date = Convert.ToDateTime(SearchText); 
                    query = query.Where(x => x.Date.Month == date.Month
                                          && x.Date.Day == date.Day
                                          && x.Date.Year == date.Year);

// Let me know if this worked for you // 如果这对你有用,请告诉我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM