Friday, April 20, 2012

Use of Select function in Dictionary

private String GetFileName(int iPageNumber)
{
try
{
var objEnm = m_objDict.Select((Pages, index) => new { index, str = Pages.Value.Contains("," + iPageNumber + ",") });

String sKey = null;
foreach (var obj in objEnm)
{

if (obj.str == true)
{
IEnumerator objKeyClc = m_objDict.Keys.GetEnumerator();
int iIndex = 0;
while (objKeyClc.MoveNext())
{
if (obj.index == iIndex++)
{
sKey = objKeyClc.Current;
break;
}
}
}

}
return sKey;
}
catch (Exception ex)
{
throw ex;
}
}

Friday, April 13, 2012

Convert MatchCollection into List

private void ConvertMatchCollectionIntoList()
{
string test = "1a2bc23AB4C15";
//string regex = "(?a)|(?b)|(?c)]";
string regex = "[\\d]+";
Regex regNoCase = new Regex(regex, RegexOptions.IgnoreCase);
Regex regCase = new Regex(regex);

MatchCollection matchNoCase = regNoCase.Matches(test);
MatchCollection matchCase = regCase.Matches(test);


List lstIndex = matchNoCase.OfType().Where(m => m.Success).ToList();
lstIndex.Sort(CompareLst);

}

public int CompareLst(Match x, Match y)
{

int iFirst =0;
int.TryParse(x.Value,out iFirst);

int iSecond;
int.TryParse(y.Value,out iSecond);
return iFirst.CompareTo(iSecond);
}

Followers

Link