とのIEnumerableを変換する際longに取っ:ToListメソッド()複数のオブジェクト
public class CountryVM
{
#region Properties
public int CountryID { get; set; }
public string Code { get; set; }
public string Name { get; set; }
#endregion
#region Constructors
/// <summary>
/// Constructor
/// </summary>
public CountryVM()
{
}
/// <summary>
/// Constructor that creates a View Model based of an Entity object
/// </summary>
/// <param name="countryCode">Fills data to View Model</param>
public CountryVM(Country_Code countryCode)
{
if (countryCode != null)
{
CountryID = countryCode.Country_Code_ID;
Code = countryCode.Country_Code1;
Name = countryCode.Country_Name;
}
}
#endregion
}
public class StateVM
{
#region Properties
public int StateID { get; set; }
public int CountryID { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public CountryVM Country { get; set; }
#endregion
#region Constructors
/// <summary>
/// Constructor
/// </summary>
public StateVM()
{
}
/// <summary>
/// Constructor that creates a View Model based of an Entity object
/// </summary>
/// <param name="stateCode">Fills data to View Model</param>
public StateVM(State_Code stateCode)
{
if (stateCode != null)
{
StateID = stateCode.State_Code_ID;
CountryID = stateCode.Country_Code_ID;
Code = stateCode.State_Code1;
Name = stateCode.State_Name;
Country = new CountryVM(stateCode.Country_Code);
}
}
#endregion
}
COUNTRY_CODEとSTATE_CODEの両方があり、私のテーブルからテーブルには、エンティティオブジェクトに変換します。
私は、次のコード行を実行します。
viewModelList = viewModelList.ToList();
を実行している場合
IEnumerable<State_Code> entityList = _stateRepository.GetAllStateCodes();
IEnumerable<StateVM> viewModelList = entityList.Select(s => new StateVM(s));
viewModelList = viewModelList.ToList();
それは、1〜3秒かかります。私はそれを試していましたが、私はStateVMオブジェクトから、
Country = new CountryVM(stateCode.Country_Code);
を削除しました。これは完璧に動作します。
StateVMがインスタンス化されると、CountryVMが内部でインスタンス化されるため、時間がかかりすぎる理由があると思います。
パフォーマンスを向上させる方法はありますか?