日期:2014-05-20  浏览次数:20797 次

Linq 查询,20万条的数据,去除有关第二个数组关键词的记录

这些是代码,

IEnumerable<string> query = null;

                string[] x = null;

                for (int i = 0; i < ImportBrand.Length; i++)
                {

                    if (i == 0)
                    {
                        query = from item in ImportKeywords
                                where (item.Contains(ImportBrand[i]) == false)
                                select item;
                        x = query.ToArray<string>();
                    }
                    else
                    {
                        query = from item in x
                                where (item.Contains(ImportBrand[i]) == false)//遍历是这个地方会                 出现超出索引界限
                                select item;
                        x = null;
                        x = query.ToArray<string>();
                    }
                }


                Console.WriteLine("====>>");
          
                ArrayList al = new ArrayList();
                foreach(string s in query)
                {
                    al.Add(s);
                }

------解决方案--------------------
本帖最后由 q107770540 于 2014-02-16 20:33:43 编辑
你的思路有点乱,else里的query变量的值,每次for循环都会被覆盖掉,之前的查询还有什么意义?

		IEnumerable<string> query = null;

          string[] x = null;

if(ImportBrand.Length>0)
{
x =     (from item in ImportKeywords
                                where !item.Contains(ImportBrand[0])
                                select item).ToArray();
             if(x != null)
{
query = from item in x
                               where ImportBrand.Any(a=>!item.Contains(a)) 
                                        select item;