How can I reduce execution time for this code block?
public static IEnumerable<ListviewDataViewModel> GetModelsList<T>(this IEnumerable<T> modelsList
, IEnumerable<ListviewColumnViewModel> modelsColumn
, Action<ListviewDataViewModel, T> listVwItemCallback = null) where T : class
{
List<ListviewDataViewModel> listItms = null;
if (modelsColumn == null || !modelsColumn.Any())
return null;
foreach (var d in modelsList)
{
var li = new ListviewDataViewModel();
foreach (var c in modelsColumn)
{
var _col = string.Empty;
var type = d.GetType();
var prp = type.GetProperty(c.ModelProperty);
if (prp != null)
_col = (prp.GetValue(d) ?? "").ToString();
li.ColumnItem.Add(_col);
}
// call the callback if available.
listVwItemCallback?.Invoke(li, d);
if (listItms == null)
listItms = new List<ListviewDataViewModel>();
listItms.Add(li);
}
return listItms;
}
When I have hundreds records in modelList, It takes about a minute to get the results. I would like to reduce the execution time to half. Can someone guide me with this?
listVwItemCallback?GetProperty()is slow,GetValue()is slow, andlistVwItemCallback.Invoke()has unknown time. Can you useExpandObjector some other technology to pull values instead of using reflection? Please expand on the use case for this and what the typicalmodelsListlooks like. Finally, doing UI operations while processing data is slow. Better to use async for processing and update the UI separately.