I am using regex code to find contact match in my windows phone application and it works good but gives an error sometimes "Values cannot be null".
try {
var l = new List<string>();
foreach (var item in listobj)
l.Add(item.FirstName);
var digitMap = new Dictionary<int, string>() {
{ 1, "" },
{ 2, "[abcABC]" },
{ 3, "[defDEF]" },
{ 4, "[ghiGHI]" },
{ 5, "[jklJKL]" },
{ 6, "[mnoMNO]" },
{ 7, "[pqrsPQRS]" },
{ 8, "[tuvTUV]" },
{ 9, "[wxyzWXYZ]" },
{ 0, "" },
};
var enteredDigits = str;
var charsAsInts = enteredDigits.ToCharArray().Select(x => int.Parse(x.ToString()));
var regexBuilder = new StringBuilder();
foreach (var val in charsAsInts)
regexBuilder.Append(digitMap[val]);
var pattern = regexBuilder.ToString();
//append a ".*" to the end of the regex to make it "StartsWith", beginning for "EndsWith", or both for "Contains";
pattern = ".*" + pattern + ".*";
var matchingNames = l.Where(x => Regex.IsMatch(x, pattern));
SearchListbox.ItemsSource = listobj.FindAll(x=>x.PhoneNumbers.Contains(str) | x.FirstName.Contains(matchingNames.FirstOrDefault()));
} catch (Exception e) {
MessageBox.Show(e.Message);
}
I think when no contacts match it throws an error, also it doesn't show all the matches until, whole name matches.
What's wrong? Thank you.