数组 与 List 相关

  string[] files1 = Directory.GetFiles(sSourceFolder, "*.txt", SearchOption.AllDirectories);// 表示 搜索 对应的目录及其所有的子目录
  string[] files2 = Directory.GetFiles(sSourceFolder, "*.csv", SearchOption.AllDirectories);
           // 如果只搜索对应目录 可用 Directory.GetFiles(sSourceFolder, "*.csv");
  List<string> lstFiles = new List<string>();
  lstFiles.AddRange(files1);
  lstFiles.AddRange(files2);


取多种类型的文件

        public static string[] GetFiles(string sourceFolder, string filters, SearchOption searchOption)
        {
            return filters.Split('|').SelectMany(filter => Directory.GetFiles(sourceFolder, filter, searchOption)).ToArray();
        }

        public static List<string> GetLFiles(string sourceFolder, string filters, SearchOption searchOption)
        {
            return filters.Split('|').SelectMany(filter => Directory.GetFiles(sourceFolder, filter, searchOption)).ToList();
        }
调用样例:

 string sFolder=@"your files folder";
  string[] arrTmp = Common.GetFiles(sFolder, "FileA_837*.x12|FileB_837*.x12|Remove_837*.x12", SearchOption.TopDirectoryOnly);
    List<string> lstTmp = Common.GetLFiles(sFolder, "FileA_837*.x12|FileB_837*.x12|Remove_837*.x12", SearchOption.TopDirectoryOnly);

   // arrTmp 与 lstTmp 2个集合中的内容是一样的。
          


2个List取差集

//demo1:

                List<string> lstA = new List<string> { "abc", "aac", "adc", "bbcc" };
                List<string> lstB = new List<string> { "abc", "bbcc" };
                IEnumerable<string> result1 = lstA.Except<string>(lstA.Intersect<string>(lstB));
                List<string> lstC = new List<string>(result1);  // get aac, adc


// 以下demo2 和 demo3中用到的 方法 ExceptWith:

 static List<T> ExceptWith<T>(List<T> lstFrom, List<T> lstExcept) where T : IComparable
        {
            List<T> lstResult = new List<T>();
            int iFromIndex, iExceptIndex, iCopyIndex;
            iFromIndex = iExceptIndex = iCopyIndex = 0;

            lstFrom.Sort();
            lstExcept.Sort();

            while ((iFromIndex < lstFrom.Count) && (iExceptIndex < lstExcept.Count))
            {
                int compar = ((IComparable)(lstFrom[iFromIndex].ToString().ToUpper()))
                    .CompareTo(lstExcept[iExceptIndex].ToString().ToUpper());
                if (compar < 0)
                {
                    iFromIndex++;
                }
                else if (compar > 0)
                {
                    iExceptIndex++;
                }
                else if (compar == 0)
                {
                    if (iFromIndex > iCopyIndex)
                        CopyData(lstFrom, lstResult, iCopyIndex, iFromIndex);
                    iFromIndex++;
                    iCopyIndex = iFromIndex;

                }
            }
            CopyData(lstFrom, lstResult, iCopyIndex, lstFrom.Count);
            return lstResult;
        }

        static void CopyData<T>(List<T> from, List<T> to, int startindex, int endindex)
        {
            if (from == null || to == null || startindex < 0 || endindex < 0 || endindex <= startindex) return;
            to.AddRange(from.Where((data, index) => index >= startindex && index < endindex));
        }


//demo2:

                List<string> lstA = new List<string> { "abc", "aac", "adc", "bbcc", "ddef", "bbcde" };
                List<string> lstB = new List<string> { "abc", "bbcc", "DDEf", "bbcdf" };
                List<string> lstC = lstA.Except<string>(lstA.Intersect<string>(lstB)).ToList(); // get aac,adc,ddef,bbcde
                lstC = ExceptWith(lstA,lstB).ToList(); // get aac, adc, bbcde
 

//demo3:

                List<double> lstA = new List<double> { 1.1, 4.4, 2.2, 1.5, 6.9, 9.57, 6.9, 5.93, 5.930, 8.3, 4.120 };
                List<double> lstB = new List<double> { 2.2, 1.1, 1.5, 8.668, 8.30, 4.12 };
                List<double> lstC = lstA.Except<double>(lstA.Intersect<double>(lstB)).ToList(); // get 4.4, 6.9, 9.57, 5.93

                lstC = ExceptWith(lstA, lstB).ToList(); // get 4.4, 5.93, 5.93, 6.9, 6.9, 9.57



数组使用contains

http://blog.sina.com.cn/s/blog_4c142e330100fr0c.html


using System.Collections;

string[] strArr = {"a","b","c","d","e"};
bool exists = ((IList)strArr).Contains("a");


C# Linq获取两个List或数组的差集交集

http://www.cnblogs.com/greatverve/archive/2012/03/29/csharp-list-linq-Intersection.html


C#中如何判断list是否完整包含另一个list  

http://blog.youkuaiyun.com/gulingeagle/article/details/8598016


[C#小技巧收集]将字符串转换成List<T>

http://www.cnblogs.com/wenjian/archive/2009/08/06/1540112.html



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值