Scott Gu Dynamic Linq Convert Datetime to ShortDatetime

asp.net c# dynamic-linq linq tsql

Question

I am using the Scott Gu Dynamic Linq class and I am trying to convert a datetime to return a short datetime before databinding the result set to a gridview in ASP.net. Now, I know that I can go through the gridview row databound event and check each cell to see if it is a date, and if it is, convert the date to the shortdate, however that is very cumbersome and I do not feel it is very efficient.

I am building my select statement in DynamicLinq as follows:

Filter gets its information as follows:

 foreach (KeyValuePair<string,string> kvp in dictFilters)
        {
            filter += kvp.Key + " as " + kvp.Value.Replace("-", "_").Replace(" ", "_") + ",";
        }


        var result = db.ViewADHOCContractInfos.Select("new(" + filter + ")");

which gives me the result set of:

    {SELECT [t0].[dtmAward] AS [Award_Date], [t0].[guidFromId],  [t0].[strFundingNumber] AS [Funding_Number], [t0].[dtmCertified] AS [Certified_Date], [t0].[strFundingNumberStatus] AS [Funding_Status]
FROM [dbo].[ViewADHOCInfo] AS [t0]
}

Now what I want is this:

   {SELECT Convert(VarChar, [t0].[dtmAward], 101) AS [Award_Date], [t0].[guidFromId],  [t0].[strFundingNumber] AS [Funding_Number], [t0].[dtmCertified] AS [Certified_Date], [t0].[strFundingNumberStatus] AS [Funding_Status]
FROM [dbo].[ViewADHOCInfo] AS [t0]
}

Is it possible to add a SQL convert to by select statement using Dynamic Linq?

1
0
8/28/2012 1:59:48 PM

Popular Answer

Yours is a formatting issue:

Simply set the DataFormatString property on the column as so:

DataFormatString="{0:MM/dd/yyy}"

The full context:

<asp:BoundField DataField="DateColumn" 
                    HeaderText="Date" 
                    DataFormatString="{0:MM/dd/yy}" />

Or to any suitable format for your case and the date will be formatted properly.

1
8/28/2012 1:55:26 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow