Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert datetime to string in linqtosql?

Tags:

linq-to-sql

I am using linqtosql and inside of linq query, I tried to convert datetime type column to string like 'dd-MM-yy'. However, I got error as following :

NotSupportedException: Method 'System.String ToString(System.String)' has no supported translation to SQL.

following is my linq query:

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) }
like image 777
Ray Avatar asked Jun 16 '10 10:06

Ray


3 Answers

You can use string.Format than ToString() method to solve your problem. Like

ColumnTimeForXCategory = string.Format("{0:dd-MM-yy}",ffv.Column_time.Value) 
like image 132
Zain Ali Avatar answered Nov 23 '22 03:11

Zain Ali


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

CreatedDateTime = 
   modelChartArea.CreatedDateTime.Month + "/" + 
   modelChartArea.CreatedDateTime.Day + "/" + 
   modelChartArea.CreatedDateTime.Year
like image 39
Shane M. Cherniss Avatar answered Nov 23 '22 02:11

Shane M. Cherniss


This maybe late but still....try using .AsEnumerable()...I also somehow stumbled on this problem then I found this: http://forum.linqpad.net/discussion/58/comparing-dates-in-linqpad

like image 24
JYR Avatar answered Nov 23 '22 02:11

JYR