Datatables Compute Count(xxxx) with filters using Guid datatype error

K

kristoffer

I have done some testing on the DataTable.Compute method in C# 2.0
where i am using the filter parameter on a column of datatype
System.Guid, this seems to do some fun stuff like NOT WORKING!!!!
Please try the console app code below and tell me if i am wrong or
...... I put 100000 Guids into a datatable, creates an array with the
same 100000 values and adds another 100000 new values. Then i run a
compute to find the first 100000 with matching values. It works when
using a hashtable but it does not return the correct number when using
the datatable.compute:


using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Collections;

namespace ConsoleApplication1
{
class Program
{
static int imax = 100000;
static void Main(string[] args)
{
DataSet dataset = new DataSet();
DateTime startTime = DateTime.Now;
Console.WriteLine("Start populating Data");
TimeSpan ts;
dataset.Tables.Add(new DataTable());
dataset.Tables[0].Columns.Add(new DataColumn("DataItemID",
typeof(Int32)));
dataset.Tables[0].Columns.Add(new
DataColumn("DataItemGuid", typeof(Guid)));
for (int i = 0; i < imax; i++)
{
dataset.Tables[0].Rows.Add(new object[2] { i,
Guid.NewGuid() });
}

string[] arrayOfItems = new
string[dataset.Tables[0].Rows.Count * 2];
Hashtable hashtableWithItems = new Hashtable();

for (int i = 0; i < dataset.Tables[0].Rows.Count; i++)
{
arrayOfItems =
dataset.Tables[0].Rows[1].ToString();
}
for (int i = 0; i < dataset.Tables[0].Rows.Count; i++)
{
arrayOfItems[dataset.Tables[0].Rows.Count + i] =
Guid.NewGuid().ToString();
}

for (int i = 0; i < dataset.Tables[0].Rows.Count; i++)
{

hashtableWithItems[dataset.Tables[0].Rows[1].ToString()] = i;
}
for (int i = 0; i < dataset.Tables[0].Rows.Count; i++)
{
hashtableWithItems[Guid.NewGuid().ToString()] =
dataset.Tables[0].Rows.Count + i;
}


ts = DateTime.Now.Subtract(startTime);
Console.WriteLine("Start populating Data, took " +
ts.ToString());
Console.WriteLine("Table.Rows.Count " +
dataset.Tables[0].Rows.Count.ToString());
Console.WriteLine("ArrayOfItems.Count " +
arrayOfItems.Length.ToString());
Console.WriteLine("HashtableWithItems.Count " +
hashtableWithItems.Count.ToString());

Console.WriteLine("------------------------------------------------------");
RunTest(dataset, arrayOfItems, hashtableWithItems);

Console.WriteLine("------------------------------------------------------");
Console.WriteLine("Hit enter to run test again / type exit
to quit application");

Console.WriteLine("------------------------------------------------------");
while (Console.ReadLine() != "exit")
RunTest(dataset, arrayOfItems, hashtableWithItems);

}

private static void RunTest(DataSet dataset, string[]
arrayOfItems, Hashtable hashtableWithItems)
{
DateTime startTime = DateTime.Now;
int itemsremoved = 0;
foreach (string item in arrayOfItems)
{
try
{
int i = 0;
try
{
i =
(int)dataset.Tables[0].Compute("Count(DataItemID)", "DataItemGuid='" +
item + "'");
}
catch (Exception ex)
{
Console.WriteLine("item[" + item + "] failed to
Compute [" + ex.Message + "]");
}
if (i == 0)
{
itemsremoved++;
}
}
catch (Exception e)
{
Console.WriteLine("item[" + item + "] failed to
remove [" + e.Message + "]");
}
}
Console.WriteLine("DataSet Compared with removal of " +
itemsremoved + " items, took " +
DateTime.Now.Subtract(startTime).ToString());
startTime = DateTime.Now;
itemsremoved = 0;
foreach (string item in arrayOfItems)
{
try
{

if (hashtableWithItems.Contains(item))
{
itemsremoved++;
// Console.WriteLine("item[" + item + "] was
removed" );
}
}
catch (Exception e)
{
Console.WriteLine("hashtableWithItems[" + item + "]
failed to remove [" + e.Message + "]");
}
}
Console.WriteLine("Hashtable Compared with removal of " +
itemsremoved + " items, took " +
DateTime.Now.Subtract(startTime).ToString());
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top