I am struggling with a nullable datetime column [DateInsp] in an ASP.NET app which uses SubSonic3, Linq, MS SQL Server 2005.
I had this all working when the datetime column [DateInsp] did not allow nulls. A new requirement forced me to set the [DateInsp] column to allow nulls and now I am struggling getting this piece of functionality to work properly again.
Problem 1: I need to first render a dropdown list of the 7 most recent inspection dates for a given inspector (this is a grouped list of the 7 most recent dates for the inspector). Here is the TSQL that I need to convert to Linq syntax:
declare @InspectorID varchar(5)
set @InspectorID = 'GPA'
select top 7 convert(nvarchar(30), [DateInsp], 101) InspectedDate
from [dbo].[IncomingInspection]
group by [InspectorID], convert(nvarchar(30), [DateInsp], 101)
having [InspectorID] = @InspectorID
order by convert(nvarchar(30), [DateInsp], 101) desc
If I can't get this work properly using Linq, BUT I can/could build a stored proc to return the list of dates and throw that into a dropdown. Fair enough. I've been fighting with the Linq syntax so any help in this area is greatly appreciated.
Problem 2: I need to use the selected date in the dropdown mentioned above to pull the correct records for this inspector and the correct date. Again, this is a nullable datetime field and this is real sticking point for me.
Here was the original Linq syntax that accomplished the requirement before I had to change the datetime field to allow Nulls:
Dim query = (From i In db.IncomingInspections _
Where i.InspectorID = User.Identity.Name _
Group By Key = New With {i.DateInsp} Into Group _
Order By Key.DateInsp Descending _
Select Key = New With {.DateInsp = Key.DateInsp.ToShortDateString}).Take(7)
This app is written in VB.NET and uses some Linq syntax which both make my head hurt. If you can offer C# syntax to accomplish this I can translate it to VB.NET myself.
EDIT:
For some reason I can't use the .Value property; I get: The member 'Value' is not supported
I get: "Incorrect syntax near '<'." if I try to add the date comparison to the where clause too.
END EDIT
Thanks Much,
Josh