[英]Perform search by Date using entity framework
I try to make search query in Entity framework. 我尝试在实体框架中进行搜索查询。 I have table Project with 'Id', 'ProjectName', 'Description' and "modificationdate" etc. I want to perform search on 'ProjecName', 'Description' and "modificationdate". 我的表Project带有“ Id”,“ ProjectName”,“ Description”和“ modificationdate”等。我想搜索“ ProjecName”,“ Description”和“ modificationdate”。 I successfully perform search on 'ProjectName' & 'Description' but i don't know how perform search by date 我成功地在“ ProjectName”和“ Description”上执行了搜索,但是我不知道如何按日期执行搜索
var found = from n in context.Project
where n.projectName.Contains(SearchKey) ||
n.description.Contains(SearchKey)
select n;
You can use the greather-than >
, less-than <
and equals ==
operators for example (see this for a complete list) to search by date. 您可以使用例如大于号>
,小于号<
和等于==
运算符(请参阅此以获得完整列表)来按日期搜索。 Example to get projects modified a week ago or less: 一周或更短时间内修改项目的示例:
DateTime.Now.AddDays(-7) > n.modificationdate
If you don't want to include the time, you can use the Date
property to exclude it: 如果您不想包括时间,则可以使用Date
属性将其排除:
DateTime.Now.AddDays(-7).Date > n.modificationdate.Date
Edit: search by given date: 编辑:按给定日期搜索:
date == n.modificationdate
Where date
is the given date. date
是给定的日期。 If the date is specified in the SearchKey
variable (which I assume is a string
), you have to parse it to a date first. 如果在SearchKey
变量(我假设是string
)中指定了日期,则必须先将其解析为日期。
To take what you're saying from the comments: 从评论中得出您的意思:
Actually i try to perform search on given date 实际上,我尝试在给定日期执行搜索
If you want to search for modifications on a given date, just search for values greater than or equal to the start of the date and then everything less than the day after. 如果要搜索给定日期的修改,只需搜索大于或等于日期开始的值,然后搜索小于第二天的所有值。
var searchDate = new DateTime(2013, 2, 5); // or searchDateWithTime.Date
var searchDatePlusOne = searchDate.AddDays(1);
return from n in context.Project
where n.ModificationDate >= searchDate
&& n.ModificationDate < searchDatePlusOne
select n;
If n.ModificationDate
contains a date and nothing more, then n.ModificationDate == searchDate
is a sufficient predicate. 如果n.ModificationDate
包含日期,则n.ModificationDate == searchDate
是足够的谓词。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.