首页 新闻 赞助 找找看

如何用C#过滤重复数据

0
悬赏园豆:20 [已解决问题] 解决于 2012-06-01 21:08

在记事本里有很多重复数据,如下

99999@126.com

111111@126.com

22222@126.com

99999@126.com

111111@126.com

99999@126.com

99999@126.com

22222@126.com

99999@126.com

111111@126.com

99999@126.com

111111@126.com

99999@126.com

肉眼一看,就知道是3条重复的数据,我的问题就是如何过滤这些数据,最后只剩下3条。

注意实际数据非常多,格式就是上面的一样,每行一条邮箱地址。数据重复的位置不定。

我用的语言是C#,

无需数据库,写关键代码即可。

我欲成仙快乐齐天的主页 我欲成仙快乐齐天 | 初学一级 | 园豆:17
提问于:2012-06-01 17:31
< >
分享
最佳答案
1

法一:利用数据库的查询,利用select distinct,参见http://q.cnblogs.com/q/24271/

法二:利用List的Contains方法,如果不包含该元素则加入List,参见http://www.cnblogs.com/codemo/archive/2012/06/01/MailDataFilter.html

收获园豆:18
代码示例 | 初学一级 |园豆:49 | 2012-06-01 20:48

灰常感谢,你还厉害了。

我欲成仙快乐齐天 | 园豆:17 (初学一级) | 2012-06-01 21:08
其他回答(1)
2
            System.IO.FileInfo fileInfo = new System.IO.FileInfo("");
            System.Windows.Forms.TextBox box = new System.Windows.Forms.TextBox();
            using (System.IO.StreamReader reader = fileInfo.OpenText())
            {
                box.Text = reader.ReadToEnd();
            }
            var query = from line in box.Lines group line by line into lines where lines.Count() > 1 select lines.Key;


方法二:

跟方法一类似,不过不使用TextBox,而是使用集合(使用StreamReader的ReadLine逐条读取到一个集合中)

 

方法三:

定义一个枚举类,实现逐行读取:

    class TextFileLines : IEnumerable<string>, IEnumerator<string>
    {
        StreamReader _reader;
        public TextFileLines(StreamReader reader)
        {
            _reader = reader;
        }

        IEnumerator<string> IEnumerable<string>.GetEnumerator()
        {
            string line;
            while ((line = _reader.ReadLine()) != null)
            {
                yield return line;
            }
        }
    }
            System.IO.FileInfo fileInfo = new System.IO.FileInfo("");
            string[] lines;
            using (System.IO.StreamReader reader = fileInfo.OpenText())
            {
                lines = (from l in new TextFileLines(reader) group l by l into g where g.Count() > 1 select g.Key).ToArray();
            }

 

方法一:

收获园豆:2
无之无 | 园豆:5095 (大侠五级) | 2012-06-01 17:50
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册