performing more than one Where in query return null!!! why? how to fix this?

Posted by Sadegh on Stack Overflow See other posts from Stack Overflow or by Sadegh
Published on 2010-05-29T11:42:00Z Indexed on 2010/05/29 12:52 UTC
Read the original article Hit count: 236

Filed under:

hi, i have wrote a method that filters output with provided query and return it. when one Where excuted; it return correct output but when more than one Where excuted; output is null and Exception occured with message "Enumeration yielded no results". why? how i can fix it?

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;
    }

thanks in advance ;)

© Stack Overflow or respective owner

Related posts about linq-to-sql