views:

36

answers:

1

I have SQL data table that containes a DATE or DATETIME field

a LINQ statement show here just to get the basic idea

var testLinq = from t in DBDataContext.Certificates select t;

    foreach (var t in testLinq)
    {
     ....
    }

Left out the code in {..} for briefness. BUT whenever 'foreach' tries to use t I get this exception

"Input string was not in a correct format." When converting a string to DateTime, parse the string to take the date before putting each variable into the DateTime Object

How can I do this when its handled by Linq inside the foreach loop?

A: 

First of all, make it like this

var testLinq = (from t in DBDataContext.Certificates select t).ToList();

foreach (var t in testLinq)
{
 ....
}

and convert your string to to datetime like this

DateTime dt = DateTime.Parse("yourString")

if its not working,

DateTime dt = new DateTime(year,month,day);

and get your year,month,day values with Convert.ToInt32(yourString.SubString(0,4)) etc

Serkan Hekimoglu