List 集合的交集

   private void Test()
        {
            List<string> lsA = new List<string>();
            lsA.Add("A");
            lsA.Add("B");
            lsA.Add("C");
            lsA.Add("D");

            List<string> lsB = new List<string>();
            lsB.Add("C");
            lsB.Add("D");
            lsB.Add("E");
            lsB.Add("F");


            List<string> lsC = new List<string>();
            lsC.Add("D");
            lsC.Add("G");
            lsC.Add("H");

            IEnumerable<string> lstNew = null;
            lstNew = lsA.Intersect(lsB, StringComparer.OrdinalIgnoreCase);

            lstNew = lstNew.Intersect(lsC, StringComparer.OrdinalIgnoreCase);
            //也可以连着写,如下
            //   lstNew = lsA.Intersect(lsB, StringComparer.OrdinalIgnoreCase).Intersect(lsC, StringComparer.OrdinalIgnoreCase);


            //下面这种方式也可以
            // IEnumerable<string> lsIntersect = Enumerable.Intersect(lsA, lsB);

            
            string str = string.Empty;
            if (lstNew != null && lstNew.Count<string>() > 0)
            {
                foreach (string c in lstNew)
                {
                    str += c;
                }
            }
            MessageBox.Show(str);
        }

 

posted @ 2014-01-23 20:25  quietwalk  阅读(469)  评论(0编辑  收藏  举报