Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 4.0 有效地验证大型对象列表_C# 4.0 - Fatal编程技术网

C# 4.0 有效地验证大型对象列表

C# 4.0 有效地验证大型对象列表,c#-4.0,C# 4.0,我有一个功能,可以在某个字段未通过电子邮件或电话验证检查时从集合中删除项目,但这在本文中并不重要。问题是正则表达式的速度相对较慢,我有100多万个项目的列表 我的职能 public HashSet<ListItemModel> RemoveInvalid(HashSet<ListItemModel> listItems) { string pattern = (this.phoneOrEmail == "email")//phone

我有一个功能,可以在某个字段未通过电子邮件或电话验证检查时从集合中删除项目,但这在本文中并不重要。问题是正则表达式的速度相对较慢,我有100多万个项目的列表

我的职能

public HashSet<ListItemModel> RemoveInvalid(HashSet<ListItemModel> listItems)
        {
            string pattern = (this.phoneOrEmail == "email")//phoneOrEmail is set via config file 
                ?
                //RFC 5322 compliant email regex. see http://www.regular-expressions.info/email.html
@"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?" 
:
                //north-american phone number regex. see http://stackoverflow.com/questions/12101125/regex-to-allow-only-digits-hypens-space-parentheses-and-should-end-with-a-dig
@"(?:\d{3}(?:\d{7}|\-\d{3}\-\d{4}))|(?:\(\d{3}\)(?:\-\d{3}\-)|(?: \d{3} )\d{4})";

            Regex re = new Regex(pattern);
            if (phoneOrEmail == "email")
            {
                return new HashSet<ListItemModel>(listItems.Where(x => re.IsMatch(x.Email,0)));
            }
            else
            {
                return new HashSet<ListItemModel>(listItems.Where(x => re.IsMatch(x.Tel, 0)));
            }
        }
这需要很长时间才能执行。是否有更快的方法返回仅包含有效电子邮件/电话号码的子集


我需要想出一个闪电般快的办法。我的其他操作通常只需要几秒钟就可以处理700k+的项目,但这种方法需要花费很长时间,我讨厌这样。我将尝试一系列LINQ.Containsx、y、z检查,但同时,我希望比我聪明的人提供一些信息。

您尝试过Asparel扩展方法和预编译正则表达式吗?由于某些原因,Asparel实际上需要更长的时间。预编译的正则表达式要快一点。