执行多个查询返回null!为什么?怎么解决这个问题?

时间:2010-05-29 11:42:01

标签: linq-to-sql

我写了一个方法,用提供的查询过滤输出并返回它。当一个人在哪里;它返回正确的输出但是当多于一个时执行;输出为空并且出现异常,并显示消息“枚举未产生结果”。为什么?我怎么能解决它?

public IQueryable<SearchResult> PerformSearch(string query, int skip = 0, int take = 5)
    {
        if (!string.IsNullOrEmpty(query))
        {
            var queryList = query.Split('+').ToList();
            var results = GENERATERESULTS();
            string key;
            foreach (string _q in queryList)
            {
                if (_q.StartsWith("(") && _q.EndsWith(")"))
                {
                    key = _q.Replace("(", "").Replace(")", "");
                    results = results.Where(q => q.Title.Contains(key, StringComparison.CurrentCultureIgnoreCase));
                }
                else if (_q.StartsWith("\"") && _q.EndsWith("\""))
                {
                    key = _q.Replace("\"", "").Replace("\"", "");
                    results = results.Where(q => q.Title.Contains(key, StringComparison.CurrentCulture));
                }
                else if (_q.StartsWith("-(") && _q.EndsWith(")"))
                {
                    key = _q.Replace("-(", "").Replace(")", "");
                    results = results.Where(q=> !q.Title.Contains(key, StringComparison.CurrentCultureIgnoreCase));
                }
                else
                {
                    key = _q;
                    results = results.Where(q => q.Title.Contains(key, StringComparison.CurrentCulture));
                }
            }
            this._Count = results.Count();
            results = results.Skip(skip).Take(take);
            this._EndOn = DateTime.Now;
            this.ExecutionTime();
            return results;
        }
        else return null;
    }

提前感谢;)

1 个答案:

答案 0 :(得分:1)

        string key; 
        foreach (string _q in queryList) 
        { 

啊,表达式绑定到key变量。每次循环执行都需要一个新的key变量。

        foreach (string _q in queryList) 
        { 
           string key;