由于逐行反射相当昂贵,我一直在寻找构建和插入实体的更快的替代方案。我对这个主题进行了一些研究research ,并且还发现了一些性能comparisons,它们似乎表明表达树是可行的方法。我将如何重写以下功能以利用此功能?
/// <summary>
/// EVENT HANDLER: Handles the RowDataBound event of the customerSearchResultsGridView control.
///
/// Allow the entire row to be clicked as a "Selector".
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="GridViewRowEventArgs"/> instance containing the event data.</param>
protected void searchResultsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onclick"] =
ClientScript.GetPostBackClientHyperlink(searchResultsGridView, "Select$" + e.Row.RowIndex);
}
}
protected void searchResultsGridView_SelectedIndexChanged(object sender, EventArgs e)
{
aboutProductBtnPanel.Enabled = (searchResultsGridView.SelectedRow == null) ? false : true;
}
/// <summary>
/// EVENT HANDLER: Handles the PageIndexChanging event of the searchResultsGridView control.
///
/// Increment the next page of the GridView for results > 15.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="GridViewPageEventArgs"/> instance containing the event data.</param>
protected void searchResultsGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
searchResultsGridView.PageIndex = e.NewPageIndex;
// don't refetch the data, just get it from the session
searchResultsGridView.DataSource = SessionWrapper.Current.CaptureProductSearchResults;
searchResultsGridView.DataBind();
// new page, clear the selection
searchResultsGridView.SelectedIndex = -1;
aboutProductBtnPanel.Enabled = false;
}
编辑:
以下是我最终实施的解决方案:
public static void InsertTable(IEnumerable<DataTable> chunkedTable)
{
Parallel.ForEach(
chunkedTable,
new ParallelOptions
{
MaxDegreeOfParallelism = Convert.ToInt32(ConfigurationManager.AppSettings["MaxThreads"])
},
chunk =>
{
Realty_Records_ProdEntities entities = null;
try
{
entities = new Realty_Records_ProdEntities();
entities.Configuration.AutoDetectChangesEnabled = false;
foreach (DataRow dr in chunk.Rows)
{
var parcelToInsert = new Parcel();
foreach (DataColumn c in dr.Table.Columns)
{
var propertyInfo = parcelToInsert.GetType()
.GetProperty(
c.ColumnName,
BindingFlags.SetProperty | BindingFlags.IgnoreCase
| BindingFlags.Public | BindingFlags.Instance);
propertyInfo?.SetValue(
parcelToInsert,
TaxDataFunction.ChangeType(
dr[c.ColumnName],
propertyInfo.PropertyType),
null);
}
entities.Parcels.Add(parcelToInsert);
}
entities.SaveChanges();
}
catch (Exception ex)
{
TaxDataError.AddTaxApplicationLog(
TaxDataConstant.CategoryError,
ex.Source,
ex.Message,
ex.StackTrace);
throw;
}
finally
{
entities?.Dispose();
}
});
}
答案 0 :(得分:3)
SetProperty(parcelToInsert, c.ColumnName, dr[c.ColumnName])
...用法
{{1}}