[置顶] 用Linq快速删除重复的元素的方法

实现代码如下:
private void button8_Click( object sender, EventArgs e) 
{
     string[] myStrs = { "lihao", "lixiaodaoaaa", "lili", "meinv", "lixiaodaoaaa", "geili"
                          , "sb", "nihao", "sunray", "sss", "lihao", "geilideren", "bushihaoren"
                          , "sunrrr", "sunray", "nishuone", "ceshi"};
    IEnumerable < string > strNames = myStrs.Distinct().OrderBy(p = >p.Length);
     foreach ( string str in strNames)
    {
        listBox1.Items.Add(str);
    }
}
private void button7_Click( object sender, EventArgs e) 
{
     string[] myStrs = { "lihaoDaniu", "lixiaodaoahenniubi", "nimeimeiLoveYou", "iloveyousb", "good", "giveme"
                          , "loveeeeeeeeeeeeeeeeee", "shaa", "bushi", "String", "liulongq", "libo", "hanleilei"
                          , "nishi", "geilibam", "aobabaM", "cheerUp", "chili", "zicox", "zicoxNiubi", "sunrayTest"
                      , "henhao", "nihao", "woshiGehaoren", "good", "shabi", "test", "myString", "loveLe", "geili"};
     string myLen = myStrs.Length.ToString();
    MessageBox.Show(myLen);
    IEnumerable < string > myName = myStrs.Concat(myStrs);
    String str = myName.Count().ToString();
    MessageBox.Show( "连接后的字符串长度" + str);
    IEnumerable < string > strAferDistinct = myName.Distinct();
     string strN = strAferDistinct.Count().ToString();
    MessageBox.Show( "删除重复的元素后字符串长度为" + strN);
}

你可能感兴趣的:(String,object,button,LINQ)