Click here to Skip to main content
15,849,494 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

How to do Datetime UTC Convertion in linq Submit changes method in .net Application?

Thanks in Advance
Posted
Comments
Richard MacCutchan 10-Jan-14 7:45am    
Conversion to what?
suresh_ed 10-Jan-14 8:27am    
Convert client machine localtime to universaltime by getting client timezone .


I have Dataset with number of columns.I need to convert all datetime fields value to Universal time instead of coding seperately like DateTime.Now.ToUniversalTime() to each field .For that We have any method or property in LINQ Submitchanges().

1 solution

Example:

C#
var rows = ds.Tables["client"].AsEnumerable().Select(row => new { id = row["id"], utc = ((DateTime)row["dob"]).ToUniversalTime() });


This example creates a new object array list with new properties 'id' and 'utc'. You can replace this with a known class type or use to create a new list with all data in utc.

To replace all date time fields in one go, do this:
first get a result of all the date time fields,
C#
var dateTimeFields = from column in ds.Tables["client"].Columns.Cast<DataColumn>()
                                 where column.DataType == typeof(DateTime)
                                 select column;

then set the new value for each Datetime field in each row:

            foreach (var row in ds.Tables["client"].AsEnumerable())
                foreach (var field in dateTimeFields)
                    row.SetField(field.ColumnName, row.Field<DateTime>(field.ColumnName).ToUniversalTime());
 
Share this answer
 
Comments
Karthik_Mahalingam 10-Jan-14 21:24pm    
good.
but submitchanges is used in Linqtosql rite ??
you have used datatable.
njammy 10-Jan-14 21:32pm    
That's right just for illustration. In my example the rows states are set to modified so you can submitchanges back.

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