How to convert datetime to string in linqtosql?

I use linqtosql and inside the linq query, I tried to convert a datetime column to a string like 'dd-MM-yy'. However, I received an error as shown below:

NotSupportedException: The method 'System.String ToString (System.String)' does not have a supported SQL translation.

Below is my linq request:

from ffv in Flo_flowsheet_values
where ffv.Flowsheet_key == 2489
&& ffv.Variable_key == 70010558
&& ffv.Status == 'A'
&& ffv.Column_time >= DateTime.ParseExact("2010-06-13 00:00", "yyyy-MM-dd HH:mm", null)
&& ffv.Column_time <= DateTime.ParseExact("2010-06-13 22:59", "yyyy-MM-dd HH:mm", null)
select new  { 
ColumnTime = ffv.Column_time
,ColumnTimeForXCategory = ffv.Column_time.Value.ToString("dd-MM-yy") ***====> this statement invoke error***
,BTValue = Convert.ToDouble( ffv.Value) }
+5
source share
6 answers

You can use the string.Formatthan method ToString()to solve your problem. how

ColumnTimeForXCategory = string.Format("{0:dd-MM-yy}",ffv.Column_time.Value) 
+10
source

I came across this post why I was looking for the same answer. Here is what I eventually discovered that I can do by looking at the properties of the date object object.

CreatedDateTime = 
   modelChartArea.CreatedDateTime.Month + "/" + 
   modelChartArea.CreatedDateTime.Day + "/" + 
   modelChartArea.CreatedDateTime.Year
+1
+1

DateTime , LINQ to SQL # T-SQL.

- :

DateTime start 
    = DateTime.ParseExact(
        "2010-06-13 00:00", 
        "yyyy-MM-dd HH:mm", 
        CultureInfo.InvariantCulture);
DateTime end 
    = DateTime.ParseExact(
        "2010-06-13 22:59", 
        "yyyy-MM-dd HH:mm", 
        CultureInfo.InvariantCulture);

from ffv in Flo_flowsheet_values
where ffv.Flowsheet_key == 2489
    && ffv.Variable_key == 70010558
    && ffv.Status == 'A'
    && ffv.Column_time >= start
    && ffv.Column_time <= end
select new 
{ 
    ColumnTime = ffv.Column_time,
    ColumnTimeForXCategory = ffv.Column_time.Value.ToString("dd-MM-yy")
    BTValue = Convert.ToDouble( ffv.Value)
};
0

, linq , sql. . , linq.

0

Do not create a database to format strings for the user interface. Disassemble it on the client side.

0
source

All Articles