Saturday, January 29, 2011

PLINQ

List[BabyName] b = new List[BabyName]();
BabyName ob = new BabyName();
for (int i = 0; i lt 10000000; i++)
{
ob = new BabyName();
ob.Name = i.ToString();
b.Add(ob);
}
ob = new BabyName();
ob.Name = "Mahesh";
b.Add(ob);


Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
var _sequentialQuery = from b2 in b
where b2.Name.Equals("Mahesh",StringComparison.InvariantCultureIgnoreCase)
select b2;
stopWatch.Stop();

::PLINQ ::

string ss = stopWatch.ElapsedTicks.ToString();
Stopwatch stopWatch2 = new Stopwatch();
stopWatch2.Start();
var _sequentialQuery2 = from b2 in b.AsParallel().WithDegreeOfParallelism(60)
where b2.Name.Equals("Mahesh", StringComparison.InvariantCultureIgnoreCase)
select b2;
stopWatch2.Stop();
string ss2 = stopWatch2.ElapsedTicks.ToString();





class BabyName
{
public string Name { get; set; }
}

Thanks
Mahesh K Sharma

Convert the dataTable to var

var vObject = dataTable.Select().Select(t => new
{
Prop1= t["Column1"],
Prop2=
string.Concat(Convert.ToString(t["Column2"]), "(", Convert.ToString(t["Column3"]), ") ", Convert.ToString(t["Column4"])),
Prop3= t["Column5"],
Prop4= t["Column6"]
});

Thanks
Mahesh Sharma

set scrooll of div for selected item

We have a div which contains 200 radio buttons. we fixed the height of div and set the vertical scroll now we want that scroll position should automatically set according to selected item.

For this we have to find the selected vaue and get its offsetTop value and set this value to div.ScroolTop.

Code is follwoing :

var container = document.getElementById('hhhjh');
var rowToScrollTo = document.getElementById('setID7');
alert(rowToScrollTo.offsetTop);
container.scrollTop = rowToScrollTo.offsetTop;

HTML Entity from child window to parent page

Problem when we pass less than char from child window to parent then it changes &lt. after some time googling i did not find some full proof method. so i just created function to replace those vaue

function html_entity_decode(str) {

var ta=document.createElement("textarea");

ta.innerHTML=str.replace(//g,">");

return ta.value;


Thanks

Mahesh Sharma

How to get single quote & comma separated character set from datatable column: for example we want 'row1','row2','row3' from data table values

How to get single quote & comma separated character set from datatable column:
for example we want 'row1','row2','row3' from data table values but without using looping.

1)
Following method takes 3 times more execution time compare to looping
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();

String[] ArrayResIds =
Array.ConvertAll(DataTable.Select(),
delegate(DataRow row) { return (String)row["ColumnName"]; }
);
string resIDsComaaSep = string.Concat("'",string.Join(",'",ArrayResIds),"'");
sw.Stop();

private static string ConvertToString(DataRow dr) { return Convert.ToString(dr[0]); }

2)
Following method takes 35 times more execution time compare to looping

System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();

DataRow[] myrow = new DataRow[DataTable.Rows.Count];
DataTable.Rows.CopyTo(myrow, 0);
string[] ArrayResIds = Array.ConvertAll(myrow, new Converter(ConvertToString));

string resIDsComaaSep = string.Concat("'", string.Join(",'", ArrayResIds), "'");
sw.Stop();
string timeconsuming = sw.ElapsedTicks.ToString();

3) If oracle or sql available for this functionality than it easy to get comma separated string

--Sample table schema

Create table SchemaID([ID] smallint,SchemaID int NOT NULL)

--Dummy insert statements

Insert into SchemaID values (1,12)Insert into SchemaID values (1,13)Insert into SchemaID values (1,14)Insert into SchemaID values (2,15)Insert into SchemaID values (2,16)Insert into SchemaID values (2,17)Insert into SchemaID values (2,18)

--Solution

Declare @ID varchar(100)
Select @id=Coalesce(@ID + ', ', '') + Cast(SchemaID AS varchar(5)) From SchemaID Where [ID] = 1 SELECT @ID