2

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". I successfully perform search on 'ProjectName' & 'Description' but i don't know how perform search by date

var found = from n in context.Project
                            where n.projectName.Contains(SearchKey) ||
                            n.description.Contains(SearchKey) 
                        select n;
Henk Mollema
  • 44,194
  • 12
  • 93
  • 104
Vicky
  • 61
  • 4

2 Answers2

2

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:

DateTime.Now.AddDays(-7).Date > n.modificationdate.Date

Edit: search by given date:

date == n.modificationdate

Where date is the given 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.

Henk Mollema
  • 44,194
  • 12
  • 93
  • 104
  • This would be inaccurate without truncating the time as is if you search at 2pm, the search will not pull back items from 7 days ago at 1pm. – Lotok Oct 28 '13 at 10:12
  • The point is you are still having partial days due to time. Just truncate the time and provide a more accurate search – Lotok Oct 28 '13 at 10:13
  • I've updated my question with a note to the `Date` property of `DateTime`. – Henk Mollema Oct 28 '13 at 10:13
  • *facepalm* you still have a time on the modificationdate! – Lotok Oct 28 '13 at 10:14
  • @James A minor typo on my behalf. If you want everything that changed from yesterday (i.e. one day ago) then subtract one day and use the date and compare with `>=`. So if something was modified `27/10/2013 1:00pm` then `28/10/2013 - 1 Day` is `27/10/2013 12:00am`. There's no need for entity functions. – ta.speot.is Oct 28 '13 at 10:16
  • Actually i try to perform search on given date – Vicky Oct 28 '13 at 10:43
  • @Vicky you could use `date == n.modificationdate` for that. Where `date` is the variable containing the given date. – Henk Mollema Oct 28 '13 at 10:50
2

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.

ta.speot.is
  • 26,914
  • 8
  • 68
  • 96