Hi All,
Please find below code snippet -
cr.Id = item.ID == null ? string.Empty : ValidateValues.FindMaxLength("ITEMID", item.ID);
public static class ValidateValues
{
public static Dictionary<string, int> LengthDict;
public static string FindMaxLength(string FieldName, string Val)
{
Dictionary<string, int> FieldMaxArr = ReportFieldsArray();
int FieldLength = 0;
try
{
if (FieldMaxArr.ContainsKey(FieldName))
{
FieldLength = FieldMaxArr.Where(x => x.Key == FieldName).FirstOrDefault().Value;
}
return Val.Trim().Substring(0, Math.Min(Val.Length, FieldLength));
}
}
}
public static Dictionary<string, int> ReportFieldsArray()
{
// filling Dictionary only one
if LengthDict == null)
{
try
{
LengthDict= new Dictionary<string, int>();
LengthDict.Add("LASTNAME", 80);
LengthDict.Add("ITEMID", 80);
LengthDict.Add("FIRSTNAME", 40);
.
.
}
}
}
I am working on performance task. Here FindMaxLength() function is calling more than 500 times to calculate the values.
I want FindMaxLength() in optimized way so that It will take minimum time.
What I need to do ? Please help me.
Please find below code snippet -
cr.Id = item.ID == null ? string.Empty : ValidateValues.FindMaxLength("ITEMID", item.ID);
public static class ValidateValues
{
public static Dictionary<string, int> LengthDict;
public static string FindMaxLength(string FieldName, string Val)
{
Dictionary<string, int> FieldMaxArr = ReportFieldsArray();
int FieldLength = 0;
try
{
if (FieldMaxArr.ContainsKey(FieldName))
{
FieldLength = FieldMaxArr.Where(x => x.Key == FieldName).FirstOrDefault().Value;
}
return Val.Trim().Substring(0, Math.Min(Val.Length, FieldLength));
}
}
}
public static Dictionary<string, int> ReportFieldsArray()
{
// filling Dictionary only one
if LengthDict == null)
{
try
{
LengthDict= new Dictionary<string, int>();
LengthDict.Add("LASTNAME", 80);
LengthDict.Add("ITEMID", 80);
LengthDict.Add("FIRSTNAME", 40);
.
.
}
}
}
I am working on performance task. Here FindMaxLength() function is calling more than 500 times to calculate the values.
I want FindMaxLength() in optimized way so that It will take minimum time.
What I need to do ? Please help me.