Page 1 of 1

How to get datepart in dynamic query

Posted: Fri 27 Mar 2009 01:41
by mainship
I have a large dynamic query. Here is a much shortened version:

Dim db As New InmProfilesDataContext
Dim InmProfile = db.pr_members.AsQueryable

InmProfile = InmProfile.Where(Function(post) (post.Suspend = False))
If upperage.Text "99" Then
InmProfile = InmProfile.Where(Function(post) (post.DATE_OF_BIRTH >= DateTime.Now.AddYears(0 - CInt(upperage.Text))))
End If
If lowerage.Text "18" Then
InmProfile = InmProfile.Where(Function(post) (post.DATE_OF_BIRTH "" Then
InmProfile = InmProfile.Where(Function(post) (post.MALE_OR_FEMALE = txtGender.Text))
End If
If Location.Text "" Then
InmProfile = InmProfile.Where(Function(post) (post.STATE = Location.Text))
End If

ListView1.DataSource = InmProfile.ToList()
ListView1.DataBind()


I also want to be able to search for all records where the date is in a certain month or before or after a certain day, something like this:

InmProfile = InmProfile.Where(Function(post) (post.DATE_OF_BIRTH.month = 3))


This isn't working, but there must be a way to do this. This is VB coder by the way.

It took me over a week and a half to figure out how to get this to work, and I still don't fully understand what I'm doing. If anyone knows a good tutorial or article that can explain this well, I woul appreciate a link. But mu immediate need is how to filter on date parts.

Diane

Posted: Fri 27 Mar 2009 11:54
by AndreyR
If you have the System.Nullable (Of System.DateTime) property (in the example below it is Emp.Hiredate),
you can access it's value like in the following example:

Code: Select all

Db.Emps.Where(Function(emp) (emp.Hiredate.Value.Month = 12)

Posted: Fri 27 Mar 2009 12:45
by mainship
That did it! Thank you. I had tried

Code: Select all

Db.Emps.Where(Function(emp) (emp.Hiredate.Month = 
I was missing the '.value'. I was close but not quite there.

Thank you again,
Diane