How to get datepart in dynamic query

Discussion of open issues, suggestions and bugs regarding LinqConnect – Devart's LINQ to SQL compatible ORM
Post Reply
mainship
Posts: 6
Joined: Fri 27 Mar 2009 01:38

How to get datepart in dynamic query

Post by mainship » Fri 27 Mar 2009 01:41

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

AndreyR
Devart Team
Posts: 2919
Joined: Mon 07 Jul 2008 13:16

Post by AndreyR » Fri 27 Mar 2009 11:54

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)

mainship
Posts: 6
Joined: Fri 27 Mar 2009 01:38

Post by mainship » Fri 27 Mar 2009 12:45

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

Post Reply