Created
July 21, 2015 07:54
-
-
Save relyky/9d31a7ca13b8b1409bb8 to your computer and use it in GitHub Desktop.
自訂電話號碼比較,使用 IEqualityComparer<T>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 本人原碼截段,搭配LINQ使用。 | |
// 排除重複的電話號碼 | |
info.TEL_LIST = ""; | |
foreach(string tel in allTelList.Split(',').Distinct(new MyTelephoneEqualityComparer())) | |
info.TEL_LIST += tel + ","; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 引言:工作上需要,比較電話號碼,排除大小寫與dash | |
// 使用時搭配LINQ語法,用於 LINQ.Distinct() | |
// 參考文件:https://msdn.microsoft.com/zh-tw/library/ms132151(v=vs.110).aspx | |
/// <summary> | |
/// 自訂電話號碼比較 | |
/// </summary> | |
class MyTelephoneEqualityComparer : IEqualityComparer<string> | |
{ | |
public bool Equals(string s1, string s2) | |
{ | |
//比較時不分大小寫,排除dash'-' | |
return s1.Replace("-", "").ToUpper() == s2.Replace("-", "").ToUpper(); | |
} | |
public int GetHashCode(string s) | |
{ | |
//比較時不分大小寫,排除dash'-' | |
return s.Replace("-", "").ToUpper().GetHashCode(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment