Click here to Skip to main content
15,919,500 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The following LINQ query is giving me {1/1/0001 12:00:00 AM} for the date value instead of the date the database shows. When I copy / paste the query generated from LINQ in the debugger into SQL Management Studio, it returns the proper value.

Here is the LINQ statement:

from ga in db.GrantAgreement_Views where ga.ID.Equals(id)
                            && ga.Project_Start_Date.HasValue
                            && ga.Project_End_Date.HasValue
                            select new GAData(Convert.ToInt32(ga.ID), Convert.ToInt32(ga.Request_Amount), Convert.ToInt16(ga.Term), Convert.ToString(ga.Project_Start_Date.ToString()), Convert.ToDateTime(ga.Project_End_Date), Convert.ToString(ga.Project_Title.ToString()), (string)ga.Staff_ID.ToString(), (string)ga.Legal_Name.ToString())


Thanks for any help anyone can offer.

This is what is being passed in to SQL:
SELECT CONVERT(Int,[t0].[ID]) AS [id], CONVERT(Int,[t0].[Request_Amount]) AS [amount], CONVERT(Int,CONVERT(SmallInt,[t0].[Term])) AS [grantterm], CONVERT(NVarChar(MAX),[t0].[Project_Start_Date]) AS [start], [t0].[Project_End_Date] AS [end], CONVERT(NVarChar(MAX),[t0].[Project_Title]) AS [project], CONVERT(NVarChar(MAX),[t0].[Staff_ID]) AS [staffid], CONVERT(NVarChar(MAX),[t0].[Legal_Name]) AS [orgname]
FROM [dbo].[GrantAgreement_View] AS [t0]
WHERE ([t0].[ID] = @p0) AND ([t0].[Project_Start_Date] IS NOT NULL) AND ([t0].[Project_End_Date] IS NOT NULL)

When this is executed in SQL man studio - i get the proper results.

Here is my data struct:

public class GAData
{
    public GAData() { }
    public GAData(int id, int amount, int grantterm, DateTime start, DateTime end, string project, string staffid, string orgname)
    { ID = id; AMOUNT = amount; grantterm = GRANTTERM; start = START; end = END; PROJECT = project; STAFFID = staffid; orgname = ORGNAME; }
    public int ID;
    public int AMOUNT;
    public int GRANTTERM;
    public DateTime START;
    public DateTime END;
    public string PROJECT;
    public string STAFFID;
    public string ORGNAME;
}
Posted
Updated 13-Jul-11 13:17pm
v4
Comments
fcronin 13-Jul-11 17:45pm    
Just a thought, but did you check your GAData constructor to ensure you aren't setting that DateTime to a new DateTime?

C#
public GAData(int id, int amount, int grantterm, DateTime start, DateTime end, string project, string staffid, string orgname)
   { ID = id; AMOUNT = amount; grantterm = GRANTTERM; start = START; end = END; PROJECT = project; STAFFID = staffid; orgname = ORGNAME; }


Using all caps for some names and lowercase for others is not clear. I tend to call members things like _end. Your code passes in the value start, then has the line

start = START;

I see START is the member value you want to set. You set the passed in value to equal the member, instead of vice versa.
 
Share this answer
 
v2
Comments
Matt Kloss 13-Jul-11 19:52pm    
That was it! I feel like an idiot. Thanks for the quick response and thanks for the advice of using an underscore.
Christian Graus 13-Jul-11 19:56pm    
No worries - we all make simple mistakes like that from time to time.
I would put a breakpoint in the constructor of GAData to see what is being passed in, and what's happening from there.
 
Share this answer
 
Comments
Matt Kloss 13-Jul-11 19:18pm    
Thanks for the response. I had already done this - what goes into SQL executes correctly, I added the output into my question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900