2

I want to search by birthday but I can't do it and this what I tried:

Class:

[Required] 
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)] 
[Display(Name = "Birthdate")] 
[DataType(DataType.Date)] 
public DateTime Birthdate { get; set; }

Conditional Statement:

else if (searchBy == "Birthdate")
{
    return View(db.Class1.Where(x => x.Birthdate.Equals(Convert.ToDateTime(search)).ToList());
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Muhammad Abu Mandor
  • 295
  • 1
  • 2
  • 12
  • is there any error message ? – tschmit007 Mar 23 '16 at 13:29
  • please post more to the question to give us the best idea of how to help.. try posting the output, classes, properties, error messages, etc – Grizzly Mar 23 '16 at 14:03
  • what happens if you try `return View(db.Class1.Where(x => x.BirthDate.Date == Convert.ToDateTime(search).Date)).ToList();`? – Grizzly Mar 23 '16 at 14:23
  • Do you really want to search by exact date (including year), or do you "just" want to know who will celebrate a birthday tomorrow? – Hans Kesting Mar 23 '16 at 14:57
  • search by exact date (including year) – Muhammad Abu Mandor Mar 23 '16 at 15:00
  • i try to search with birth date and age and birth date range , i had one prop in entity mode` "[Required] [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)] [Display(Name = "Birthdate")] [DataType(DataType.Date)] public DateTime Birthdate { get; set; }" ` ` else if (searchBy == "Birthdate") { return View(db.Class1.ToList().Where(x => x.Birthdate.CompareTo(Convert.ToDateTime(search)) || search == null)); }` – Muhammad Abu Mandor Mar 23 '16 at 15:09

4 Answers4

1

Try splitting up your search date:

var day = search.Date;
var month = search.Month;
var year = search.Year;

db.Class1.Where(x => x.Birthdate.Day == day && x.Birthdate.Month == month && x.Birthdate.Year == year).ToList());
Yan
  • 403
  • 2
  • 20
0

Use :

db.Class1.Where(x => x.Birthdate.Day == Convert.ToInt32(search)).ToList());
Mah3
  • 83
  • 8
0

This is happening because you are storing Date of Birth as a DateTime. It would be much better if you just stored it as a Date, unless you're actually recording a time of birth! You can configure Entity Framework to use Date type by adding a convention.

Once you've done that, decorate your Date of Birth field:

public class Person {
    //...

    [DataType(DataType.Date)]
    public DateTime Birthdate { get; set; }

When you've done this, your search will work as you expected, since the time component is no longer stored in the database.

Community
  • 1
  • 1
Richard
  • 29,854
  • 11
  • 77
  • 120
  • can i edit in attribute after i insert data in database ? – Muhammad Abu Mandor Mar 23 '16 at 15:03
  • `[Required] [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)] [Display(Name = "Birthdate")] [DataType(DataType.Date)] public DateTime Birthdate { get; set; }` – Muhammad Abu Mandor Mar 23 '16 at 15:05
  • Yes, you can add this in with existing data. You need to have the convention from the linked answer in place for this to work. Once you do this, you'll generate a new migration to change the column data type from datetime to date. – Richard Mar 23 '16 at 15:16
  • `[Required] [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)] [DataType(DataType.Date)] public DateTime Birthdate { get; set; }` i tried that but still the same porblem – Muhammad Abu Mandor Mar 23 '16 at 16:06
-1

If you want to search by the full date:

return View(db.Class1.Where(x => x.Birthdate.Date == Convert.ToDateTime(search).Date)).ToList());
Grizzly
  • 5,873
  • 8
  • 56
  • 109
  • and when i run that " else if (searchBy == "Birthdate") { return View(db.Class1.ToList().Where(x => (DateTime.Now.Year - x.Birthdate.Year) == Convert.ToInt32(search) || search == null));" } Input string was not in a correct format. – Muhammad Abu Mandor Mar 23 '16 at 14:31
  • @MuhammadAbuMandor What? you're entire return statement has changed since original post? – Grizzly Mar 23 '16 at 14:33
  • i try to search with birth date and age and birth date range , i had one prop in entity mode "[Required] [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)] [Display(Name = "Birthdate")] [DataType(DataType.Date)] public DateTime Birthdate { get; set; }" it's my frist time to use stackoverflow so sry for messier – Muhammad Abu Mandor Mar 23 '16 at 14:48
  • @MuhammadAbuMandor No problem, please post what you just posted as a comment to me, in your original question post, along with the entire conditional if statement, not just the `else if` – Grizzly Mar 23 '16 at 14:55
  • @MuhammadAbuMandor your return statement needs to be changed to put the `.ToList()` at the end.. so `return View(db.Class1.Where(x => x.Birthdate.CompareTo(Convert.ToDateTime(search)) || search == null).ToList());` – Grizzly Mar 23 '16 at 15:15
  • Error 1 Operator '||' cannot be applied to operands of type 'int' and 'bool' – Muhammad Abu Mandor Mar 23 '16 at 15:30
  • @MuhammadAbuMandor have you tried the solution that I posted? – Grizzly Mar 23 '16 at 15:34
  • return View(db.Class1.Where(x => x.Birthdate.CompareTo(Convert.ToDateTime(search)) || search == null).ToList()); i tried this – Muhammad Abu Mandor Mar 23 '16 at 15:51
  • @MuhammadAbuMandor try `return View(db.Class1.Where(x => x.Birthdate.Date == Convert.ToDateTime(search).Date)).ToList());` – Grizzly Mar 23 '16 at 15:52