S
Sudip_inn
Guest
see the first block of code. here i am doing cross join between a list and datatable from where i am calling a function called ExtractStandardValueFromLinkText this function has many loop which take times to complete.
tell me how could i refactor the code in 2nd block to speed up the execution.
all element start with dt means datatable object and all lst means list object. i use AsParallel for querying datatable which speed up execution bit but i am looking for very good execution speed. so what way i can refactor 2nd block code which speed up the execution. if any area is not clear then ask me. so i will explain more. please share idea how best way to refactor 2nd block code.
#region 1st block
if (dtAccumulatedData.AsEnumerable().AsParallel().Any(x => !string.IsNullOrEmpty(x.Field<string>("LinkedItemList"))
&& x.Field<string>("Link").ToUpper() == "TRUE"))
{
DataTable dtFilterForLinkOnlyTrue = dtAccumulatedData.AsEnumerable().AsParallel()
.Where(x => !string.IsNullOrEmpty(x.Field<string>("LinkedItemList"))
&& x.Field<string>("Link").ToUpper() == "TRUE")
.OrderByDescending(a => a.Field<string>("Matched Section"))
.ThenBy(a => a.Field<string>("Matched Items"))
.ThenByDescending(a => a.Field<string>("Link"))
.CopyToDataTable();
//iterate in datatable of dtFilterForLink where LinkedItemList is not empty
if (dtFilterForLinkOnlyTrue != null && dtFilterForLinkOnlyTrue.Rows.Count > 0)
{
var crossjoindata = (from _dtFilterForLinkOnlyTrue in dtFilterForLinkOnlyTrue.AsEnumerable()
from p in _Periods
select new BrokerData
{
StandardValue = ExtractStandardValueFromLinkText(
(_dtFilterForLinkOnlyTrue.Field<string>("Matched Section") == null ? "" : _dtFilterForLinkOnlyTrue.Field<string>("Matched Section").ToString()),
(_dtFilterForLinkOnlyTrue.Field<string>("Matched Items") == null ? "" : _dtFilterForLinkOnlyTrue.Field<string>("Matched Items").ToString()),
p.ToString(),
(_dtFilterForLinkOnlyTrue.Field<string>("Tab") == null ? "" : _dtFilterForLinkOnlyTrue.Field<string>("Tab").ToString()),
(_dtFilterForLinkOnlyTrue.Field<string>("Row") == null ? "" : _dtFilterForLinkOnlyTrue.Field<string>("Row").ToString()),
(_dtFilterForLinkOnlyTrue.Field<string>("Units") == null ? "" : _dtFilterForLinkOnlyTrue.Field<string>("Units").ToString()),
ref dtAccumulatedPeiodicalData,
(_dtFilterForLinkOnlyTrue.Field<string>("LinkedItemList") == null ? "" : _dtFilterForLinkOnlyTrue.Field<string>("LinkedItemList").ToString()),
(_dtFilterForLinkOnlyTrue.Field<string>("Broker Items") == null ? "" : _dtFilterForLinkOnlyTrue.Field<string>("Broker Items").ToString()),
(_dtFilterForLinkOnlyTrue.Field<string>("Action") == null ? "" : _dtFilterForLinkOnlyTrue.Field<string>("Action").ToString()),
(_dtFilterForLinkOnlyTrue.Field<string>("cumulative") == null ? false :
(_dtFilterForLinkOnlyTrue.Field<string>("cumulative").ToString().ToUpper() == "TRUE" ? true : false)))
}).ToList();
}
}
lstDataCheck = null;
#endregion
#region 2nd block
private string ExtractStandardValueFromLinkText(string TabName, string StandardLineItem, string strPeriod, string BRTab, string RowNumber, string strUnits,
ref DataTable dtFilterDataFromAllData, string LinkedItemList, string BRLineItem, string strAction, bool cumulative)
{
string[] childformulas = null;
string _strBRTab = "", _RowNumber = "";
try
{
//this line prevent to consider same tabname & Lineitem in for loop
if (!lstDataCheck.Contains(BRTab + "~" + RowNumber + "~" + strPeriod))
{
//adding tabname & Lineitem in a list if tabname & Lineitem not found in lstDataCheck list
//lstDataCheck.Add(TabName + "~" + StandardLineItem + "~" + strPeriod);
lstDataCheck.Add(BRTab + "~" + RowNumber + "~" + strPeriod);
//this StandardValue will hold parent StandardValue value
StandardValue = null;
if (BRTab != "")
{
//checking parent records has StandardValue or not
if (dtFilterDataFromAllData.AsEnumerable().AsParallel().Any(w => w.Field<string>("BRTab") == BRTab
&& w.Field<string>("RowNumber") == RowNumber
&& w.Field<bool>("Link").ToString().ToUpper() == "TRUE"
&& w.Field<string>("StandardDate").Replace("A", "").Replace("E", "") == strPeriod.Replace("A", "").Replace("E", "")))
{
//storing parent StandardValue
StandardValue = dtFilterDataFromAllData.AsEnumerable().AsParallel()
.Where(w => w.Field<string>("BRTab") == BRTab
&& w.Field<string>("RowNumber") == RowNumber
&& w.Field<bool>("Link").ToString().ToUpper() == "TRUE"
&& w.Field<string>("StandardDate").Replace("A", "").Replace("E", "") == strPeriod.Replace("A", "").Replace("E", ""))
.Select(v => v.Field<string>("StandardValue"))
.FirstOrDefault();
}
}
else
{
if (dtFilterDataFromAllData.AsEnumerable().AsParallel().Any(w => w.Field<string>("TabName") == TabName
&& w.Field<string>("StandardLineItem") == StandardLineItem
&& w.Field<bool>("Link").ToString().ToUpper() == "TRUE"
&& w.Field<string>("StandardDate").Replace("A", "").Replace("E", "") == strPeriod.Replace("A", "").Replace("E", "")))
{
//storing parent StandardValue
StandardValue = dtFilterDataFromAllData.AsEnumerable().Where(w => w.Field<string>("TabName") == TabName
&& w.Field<string>("StandardLineItem") == StandardLineItem
&& w.Field<bool>("Link").ToString().ToUpper() == "TRUE"
&& w.Field<string>("StandardDate").Replace("A", "").Replace("E", "") == strPeriod.Replace("A", "").Replace("E", ""))
.Select(v => v.Field<string>("StandardValue"))
.FirstOrDefault();
}
}
//When parent StandardValue is null or empty
if (StandardValue == null || StandardValue.ToString() == "")
{
if (LinkedItemList.Trim() != "")
{
if (LinkedItemList.StartsWith("~"))
{
LinkedItemList = LinkedItemList.Substring(1, LinkedItemList.Length - 1);
}
//split link text by ~sign
childformulas = LinkedItemList.Split('~');
foreach (string cf in childformulas)
{
if (cf != "")
{
//iterate in linktext collection
_strBRTab = cf.Split('`')[1].ToString().Trim();
_RowNumber = cf.Split('`')[2].ToString().Trim();
//querying main resultset to check child records has data
ChildStandardValue = dtFilterDataFromAllData.AsEnumerable().AsParallel()
.Where(w => w.Field<string>("BRTab") == _strBRTab
&& w.Field<string>("RowNumber") == _RowNumber
&& w.Field<string>("StandardDate").Replace("A", "").Replace("E", "") == strPeriod.Replace("A", "").Replace("E", "")
&& w.Field<bool>("Link").ToString().ToUpper() == "FALSE")
.Select(v => v.Field<string>("StandardValue"))
.FirstOrDefault();
if (ChildStandardValue != null && ChildStandardValue.ToString() != "")
{
_strBRTab = string.Empty;
_RowNumber = string.Empty;
break;
}
_strBRTab = string.Empty;
_RowNumber = string.Empty;
}
_strBRTab = string.Empty;
_RowNumber = string.Empty;
}
if (ChildStandardValue != null && ChildStandardValue != "")
{
if (StandardValue == null) //when parent Standard Value not found means null
{
//here inserting new row in data table with Standard Value data
DataRow dr = dtFilterDataFromAllData.NewRow();
dr["TabName"] = TabName;
dr["StandardDate"] = strPeriod;
dr["BRTab"] = BRTab;
dr["BRLineItem"] = BRLineItem;
dr["Action"] = strAction;
dr["StandardLineItem"] = StandardLineItem;
dr["StandardValue"] = ChildStandardValue;
dr["Link"] = "True";
dr["LinkedItemList"] = LinkedItemList;
dr["RowNumber"] = RowNumber;
dr["cumulative"] = cumulative;
dtFilterDataFromAllData.Rows.Add(dr);
}
else if (StandardValue == "") //when Standard Value found but has empty data
{
//here updating Standard Value
if (BRTab != "")
{
//BRTab will not be empty when that will be direct mapping with linking
dtFilterDataFromAllData.AsEnumerable().Where(w => w.Field<string>("BRTab") == BRTab
&& w.Field<string>("RowNumber") == RowNumber
&& w.Field<bool>("Link").ToString().ToUpper() == "TRUE"
&& w.Field<string>("StandardDate").Replace("A", "").Replace("E", "") == strPeriod.Replace("A", "").Replace("E", ""))
.ForEach(w =>
{
//update parent's StandardValue with child StandardValue
w["StandardValue"] = ChildStandardValue;
});
dtFilterDataFromAllData.AcceptChanges();
}
else
{
//BRTab will be empty when that will be calculated item with linking
dtFilterDataFromAllData.AsEnumerable().Where(w => w.Field<string>("TabName") == TabName
&& w.Field<string>("StandardLineItem") == StandardLineItem
&& w.Field<bool>("Link").ToString().ToUpper() == "TRUE"
&& w.Field<string>("StandardDate").Replace("A", "").Replace("E", "") == strPeriod.Replace("A", "").Replace("E", ""))
.ForEach(w =>
{
//update parent's StandardValue with child StandardValue
w["StandardValue"] = ChildStandardValue;
});
dtFilterDataFromAllData.AcceptChanges();
}
}
}
}
}
}
}
catch(Exception ex)
{
}
return "";
}
Continue reading...
tell me how could i refactor the code in 2nd block to speed up the execution.
all element start with dt means datatable object and all lst means list object. i use AsParallel for querying datatable which speed up execution bit but i am looking for very good execution speed. so what way i can refactor 2nd block code which speed up the execution. if any area is not clear then ask me. so i will explain more. please share idea how best way to refactor 2nd block code.
#region 1st block
if (dtAccumulatedData.AsEnumerable().AsParallel().Any(x => !string.IsNullOrEmpty(x.Field<string>("LinkedItemList"))
&& x.Field<string>("Link").ToUpper() == "TRUE"))
{
DataTable dtFilterForLinkOnlyTrue = dtAccumulatedData.AsEnumerable().AsParallel()
.Where(x => !string.IsNullOrEmpty(x.Field<string>("LinkedItemList"))
&& x.Field<string>("Link").ToUpper() == "TRUE")
.OrderByDescending(a => a.Field<string>("Matched Section"))
.ThenBy(a => a.Field<string>("Matched Items"))
.ThenByDescending(a => a.Field<string>("Link"))
.CopyToDataTable();
//iterate in datatable of dtFilterForLink where LinkedItemList is not empty
if (dtFilterForLinkOnlyTrue != null && dtFilterForLinkOnlyTrue.Rows.Count > 0)
{
var crossjoindata = (from _dtFilterForLinkOnlyTrue in dtFilterForLinkOnlyTrue.AsEnumerable()
from p in _Periods
select new BrokerData
{
StandardValue = ExtractStandardValueFromLinkText(
(_dtFilterForLinkOnlyTrue.Field<string>("Matched Section") == null ? "" : _dtFilterForLinkOnlyTrue.Field<string>("Matched Section").ToString()),
(_dtFilterForLinkOnlyTrue.Field<string>("Matched Items") == null ? "" : _dtFilterForLinkOnlyTrue.Field<string>("Matched Items").ToString()),
p.ToString(),
(_dtFilterForLinkOnlyTrue.Field<string>("Tab") == null ? "" : _dtFilterForLinkOnlyTrue.Field<string>("Tab").ToString()),
(_dtFilterForLinkOnlyTrue.Field<string>("Row") == null ? "" : _dtFilterForLinkOnlyTrue.Field<string>("Row").ToString()),
(_dtFilterForLinkOnlyTrue.Field<string>("Units") == null ? "" : _dtFilterForLinkOnlyTrue.Field<string>("Units").ToString()),
ref dtAccumulatedPeiodicalData,
(_dtFilterForLinkOnlyTrue.Field<string>("LinkedItemList") == null ? "" : _dtFilterForLinkOnlyTrue.Field<string>("LinkedItemList").ToString()),
(_dtFilterForLinkOnlyTrue.Field<string>("Broker Items") == null ? "" : _dtFilterForLinkOnlyTrue.Field<string>("Broker Items").ToString()),
(_dtFilterForLinkOnlyTrue.Field<string>("Action") == null ? "" : _dtFilterForLinkOnlyTrue.Field<string>("Action").ToString()),
(_dtFilterForLinkOnlyTrue.Field<string>("cumulative") == null ? false :
(_dtFilterForLinkOnlyTrue.Field<string>("cumulative").ToString().ToUpper() == "TRUE" ? true : false)))
}).ToList();
}
}
lstDataCheck = null;
#endregion
#region 2nd block
private string ExtractStandardValueFromLinkText(string TabName, string StandardLineItem, string strPeriod, string BRTab, string RowNumber, string strUnits,
ref DataTable dtFilterDataFromAllData, string LinkedItemList, string BRLineItem, string strAction, bool cumulative)
{
string[] childformulas = null;
string _strBRTab = "", _RowNumber = "";
try
{
//this line prevent to consider same tabname & Lineitem in for loop
if (!lstDataCheck.Contains(BRTab + "~" + RowNumber + "~" + strPeriod))
{
//adding tabname & Lineitem in a list if tabname & Lineitem not found in lstDataCheck list
//lstDataCheck.Add(TabName + "~" + StandardLineItem + "~" + strPeriod);
lstDataCheck.Add(BRTab + "~" + RowNumber + "~" + strPeriod);
//this StandardValue will hold parent StandardValue value
StandardValue = null;
if (BRTab != "")
{
//checking parent records has StandardValue or not
if (dtFilterDataFromAllData.AsEnumerable().AsParallel().Any(w => w.Field<string>("BRTab") == BRTab
&& w.Field<string>("RowNumber") == RowNumber
&& w.Field<bool>("Link").ToString().ToUpper() == "TRUE"
&& w.Field<string>("StandardDate").Replace("A", "").Replace("E", "") == strPeriod.Replace("A", "").Replace("E", "")))
{
//storing parent StandardValue
StandardValue = dtFilterDataFromAllData.AsEnumerable().AsParallel()
.Where(w => w.Field<string>("BRTab") == BRTab
&& w.Field<string>("RowNumber") == RowNumber
&& w.Field<bool>("Link").ToString().ToUpper() == "TRUE"
&& w.Field<string>("StandardDate").Replace("A", "").Replace("E", "") == strPeriod.Replace("A", "").Replace("E", ""))
.Select(v => v.Field<string>("StandardValue"))
.FirstOrDefault();
}
}
else
{
if (dtFilterDataFromAllData.AsEnumerable().AsParallel().Any(w => w.Field<string>("TabName") == TabName
&& w.Field<string>("StandardLineItem") == StandardLineItem
&& w.Field<bool>("Link").ToString().ToUpper() == "TRUE"
&& w.Field<string>("StandardDate").Replace("A", "").Replace("E", "") == strPeriod.Replace("A", "").Replace("E", "")))
{
//storing parent StandardValue
StandardValue = dtFilterDataFromAllData.AsEnumerable().Where(w => w.Field<string>("TabName") == TabName
&& w.Field<string>("StandardLineItem") == StandardLineItem
&& w.Field<bool>("Link").ToString().ToUpper() == "TRUE"
&& w.Field<string>("StandardDate").Replace("A", "").Replace("E", "") == strPeriod.Replace("A", "").Replace("E", ""))
.Select(v => v.Field<string>("StandardValue"))
.FirstOrDefault();
}
}
//When parent StandardValue is null or empty
if (StandardValue == null || StandardValue.ToString() == "")
{
if (LinkedItemList.Trim() != "")
{
if (LinkedItemList.StartsWith("~"))
{
LinkedItemList = LinkedItemList.Substring(1, LinkedItemList.Length - 1);
}
//split link text by ~sign
childformulas = LinkedItemList.Split('~');
foreach (string cf in childformulas)
{
if (cf != "")
{
//iterate in linktext collection
_strBRTab = cf.Split('`')[1].ToString().Trim();
_RowNumber = cf.Split('`')[2].ToString().Trim();
//querying main resultset to check child records has data
ChildStandardValue = dtFilterDataFromAllData.AsEnumerable().AsParallel()
.Where(w => w.Field<string>("BRTab") == _strBRTab
&& w.Field<string>("RowNumber") == _RowNumber
&& w.Field<string>("StandardDate").Replace("A", "").Replace("E", "") == strPeriod.Replace("A", "").Replace("E", "")
&& w.Field<bool>("Link").ToString().ToUpper() == "FALSE")
.Select(v => v.Field<string>("StandardValue"))
.FirstOrDefault();
if (ChildStandardValue != null && ChildStandardValue.ToString() != "")
{
_strBRTab = string.Empty;
_RowNumber = string.Empty;
break;
}
_strBRTab = string.Empty;
_RowNumber = string.Empty;
}
_strBRTab = string.Empty;
_RowNumber = string.Empty;
}
if (ChildStandardValue != null && ChildStandardValue != "")
{
if (StandardValue == null) //when parent Standard Value not found means null
{
//here inserting new row in data table with Standard Value data
DataRow dr = dtFilterDataFromAllData.NewRow();
dr["TabName"] = TabName;
dr["StandardDate"] = strPeriod;
dr["BRTab"] = BRTab;
dr["BRLineItem"] = BRLineItem;
dr["Action"] = strAction;
dr["StandardLineItem"] = StandardLineItem;
dr["StandardValue"] = ChildStandardValue;
dr["Link"] = "True";
dr["LinkedItemList"] = LinkedItemList;
dr["RowNumber"] = RowNumber;
dr["cumulative"] = cumulative;
dtFilterDataFromAllData.Rows.Add(dr);
}
else if (StandardValue == "") //when Standard Value found but has empty data
{
//here updating Standard Value
if (BRTab != "")
{
//BRTab will not be empty when that will be direct mapping with linking
dtFilterDataFromAllData.AsEnumerable().Where(w => w.Field<string>("BRTab") == BRTab
&& w.Field<string>("RowNumber") == RowNumber
&& w.Field<bool>("Link").ToString().ToUpper() == "TRUE"
&& w.Field<string>("StandardDate").Replace("A", "").Replace("E", "") == strPeriod.Replace("A", "").Replace("E", ""))
.ForEach(w =>
{
//update parent's StandardValue with child StandardValue
w["StandardValue"] = ChildStandardValue;
});
dtFilterDataFromAllData.AcceptChanges();
}
else
{
//BRTab will be empty when that will be calculated item with linking
dtFilterDataFromAllData.AsEnumerable().Where(w => w.Field<string>("TabName") == TabName
&& w.Field<string>("StandardLineItem") == StandardLineItem
&& w.Field<bool>("Link").ToString().ToUpper() == "TRUE"
&& w.Field<string>("StandardDate").Replace("A", "").Replace("E", "") == strPeriod.Replace("A", "").Replace("E", ""))
.ForEach(w =>
{
//update parent's StandardValue with child StandardValue
w["StandardValue"] = ChildStandardValue;
});
dtFilterDataFromAllData.AcceptChanges();
}
}
}
}
}
}
}
catch(Exception ex)
{
}
return "";
}
Continue reading...