A journey to other dimensions and DataTables

Thursday, 15 December 2011 12:10 animaonline
You might want to convert a .NET DataTable to a multidimensional array (object[,]) for many reasons, the most common one is, to use it within Microsoft Office Suite add-ins, for example: showing the data of a DataTable in an Excel Range or a ListObject.

Many articles on the internet suggest that the best way to do this is looping trough all DataRows and columns in the DataTable and manually adding them to a multidimensional array, by pointing to the column index inside a DataRow.

That is wrong!

And one should always avoid doing that, because of the negative effect it has on the performance.

The correct thing to do would be to loop through all DataRows and columns inside the DataTable and use the built-in .NET method CopyTo() on the ItemArray of a DataRow, in order to copy column and row cells to a separate array and then in the end, merge the column and row arrays to a multidimensional array (object[,]).

When testing the conversion of a DataTable with 2088 rows and 96 columns, it only took 21 milliseconds using the CopyTo method, compared to 1200++ milliseconds if I was not using it!

The performance increase may be slightly noticeable when converting small DataTables,but imagine the effect of using the method below when converting DataTables that contain huge amounts of data...

Enjoy! :)

        private static object[,] GetRawData(DataTable dt)
        {
            //Initialize a multidimensional array that will contain our columns and rows.
            var rawData = new object[dt.Rows.Count + 1, dt.Columns.Count];

            //Initialize an array that we will copy DataTable columns to.
            var columnsArray = new object[dt.Columns.Count];

            //Initialize an array that will copy DataTable rows to.
            var rowsArray = new object[dt.Rows.Count * columnsArray.Length];

            //Copy columns from the DataTable to the columns array.
            for (var columnIndex = 0; columnIndex < columnsArray.Length; columnIndex++)
                columnsArray[columnIndex] = dt.Columns[columnIndex].ColumnName;

            //Copy rows from the DataTable to the rows array.
            for (var rowIndex = 0; rowIndex < dt.Rows.Count; rowIndex++)
                dt.Rows[rowIndex].ItemArray.CopyTo(rowsArray, rowIndex * columnsArray.Length);

            //Merge DataTable columns and rows into a 2D array.
            for (int colIndex = 0; colIndex < columnsArray.Length; colIndex++)
            {
                //Merge columns.
                rawData[0, colIndex] = columnsArray[colIndex];

                //Merge rows.
                for (var i = 0; i < rowsArray.Length / columnsArray.Length; i++)
                    rawData[i + 1, colIndex] = rowsArray[i * columnsArray.Length + colIndex];
            }

            //Done.
            return rawData;
        }
 

GTA3 Coming to Mobile Devices Next Week (December 15th)

Wednesday, 07 December 2011 16:20 animaonline

Rockstar Games is prepping to bring their smash hit Grand Theft Auto 3 to multiple iOS and Android devices.

Gamers who have been craving to get back to Liberty CIty will be happy to know that the Grand Theft Auto III: 10 Year Anniversary Edition isn’t a hacked up version of the original game, but rather it’s the full fledge game re-built for mobile devices for a mere $4.99.

Below is a list of the supported devices and some of the latest screenshots.

Apple iOS Devices: iPad 1 & 2, iPhone 4 & 4S, iPod touch 4th Generation
Android Phones: HTC Rezound, LG Optimus 2x, Motorola Atrix 4G, Motorola Droid X2, Motorola Photon 4G, Samsung Galaxy R, T-Mobile G2x
Android Tablets: Acer Iconia, Asus Eee Pad Transformer, Dell Streak 7, LG Optimus Pad, Motorola Xoom, Samsung Galaxy Tab 8.9 and 10.1, Sony Tablet S, Toshiba Thrive

 

Final Fantasy XIV changes it's free to play scheme

Tuesday, 06 December 2011 09:09 animaonline

Final Fantasy XIV goes "pay to play" as Square Enix announced today that they are going to drop the free-to-play scheme, as the release of Final Fantasy XIV Version 2.0 is nearing in.

Starting on January 6, 2012 Square Enix will commence subscription-based billing for Final Fantasy XIV services, however at a special discounted rate of £6.89 / 9.99 € until the release of Version 2.0

It will definitely be interesting to see what the future holds for the FFXIV, this action may even result in a loss of subscribers... Who knows... But one thing's for sure...

Until the 2.0 comes out I'll stick to the good old FFXI :)

 

A .NET API for Last.Fm Autocomplete Search.

Monday, 05 December 2011 10:24 animaonline

A couple of days ago I wrote a .NET API for Last.Fm's Autocomplete Search...

Make sure you have the latest fastJson libraries / source code, compile and ejoy!

 

Removing Non-Numeric Characters from a string (The easy way)

Thursday, 17 November 2011 10:38 animaonline
I was searching the web for a way to easily remove all non numeric characters from a string, but the only thing I found was some "for loop" code, which really slowed down my application, so after a little research I came up with a simple way to do it using Regular Expressions

Here it goes:

string Temp = "Hax00r L33t";
string Output = Regex.Replace(Temp, "[^0-9]", "");

 

Microsoft Accelerator - Pi Calc port in C#

Wednesday, 15 December 2010 13:48 animaonline

I'm posting the quick and dirty port of Microsoft's Accelerator π (pi calculation) sample.
The code has been ported from C++ to C# by me, a couple of years ago.

Accelerator is a high-level data parallel library which uses parallel processors such as the GPU or multicore CPU to accelerate execution.
Find out more at Microsoft Research

 

Updated site and blog

Friday, 26 November 2010 01:31 animaonline
I have updated my website and my blog CMS, and now I'm looking forward to post interesting stuff here! :)

But unfortunately it's getting late. Will do more work tomorrow.

/Roman

 

Latest tweets

about 8 hours ago #WhatGrindsMyGears the lock-screen on my #sgII is a f#inkg pain in the arse... so irresponsible... Argh!!
about 8 hours ago $ 370 for a wifi #PsVita model here in #Europe , I'm ordering one from US....
about 1 day ago Anyone know where "Gigabyte" , author of the "sharpei" #csharp might be now? #dev
about 1 day ago My Top 3 #lastfm Artists: Eminem (70), Dr. Dre (26) & Papa Roach (23) http://t.co/YPJlaiyy
about 2 days ago On my way to #work
about 3 days ago #DevTip When using NDK-Build don't forget to pass the 'clean' parameter sometimes ;) #dev #androidDev
about 7 days ago Heading to work... :)
about 9 days ago @YngveNilsen nice! Lag noe tilsvarende til Android også ;) btw.. Lagde du nettsiden selv? Den var fin!
about 9 days ago My Top 3 #lastfm Artists: Sum 41 (53), The Birthday Massacre (20) & Celldweller (11) http://t.co/YPJlaiyy
about 9 days ago ♥ Points Of Authority/99 Problems/One Step Closer - Amended Version by Jay-Z and Linkin Park #lastfm : http://t.co/fn7BG4zE
about 9 days ago I love my Visual Studio and #IntelliTrace @VisualStudio #devhttp://t.co/rEwxEs8b
about 13 days ago I earned the Go To Hell badge for Visual Studio Achievements: #VSAchievements #VisualStudio #dev http://t.co/lZNfYgkX
about 14 days ago @FinalMacstorm ok, did you like #ffxiii ?
about 14 days ago #ffxiii sucked so badly, so I'm not even thinking about picking up the sequel. Bet it sucks even more, #squareenix is on acid. #ffxiii2
about 14 days ago @FinalMacstorm so how do u like #ffxiii2 ?
about 15 days ago #pigeon #pigeons #wtf <3 http://t.co/kohc9rQg
about 16 days ago ♥ Fat Lip by Sum 41 #lastfm : http://t.co/US37HvEC amazon: http://t.co/oDLlmRDT
about 16 days ago Let's call it an evening

Tag Cloud