3

My query below checks through over 2000 categories categoriesToCheck and find articles within those categories. I guess it is too big to add to a generic list?

I'm getting an error from this line below listInitialResult.AddRange(queryableInitialResult.ToList());

namely:

An unhandled exception of type 'System.StackOverflowException' occurred

I hope anyone can suggest a good way of doing this.

if (categoriesToCheck != null && categoriesToCheck.Count() > 0)
{
    var searchPredicate = PredicateBuilder.False<T>();

    foreach (ID category in categoriesToCheck)
    {
        var categoryToCompare = category.ToString().Replace("{", "").Replace("}", "");
        searchPredicate = searchPredicate.Or(i => i.Tags.Contains(categoryToCompare));
    }

    queryableInitialResult = queryable.Where(searchPredicate);

    listInitialResult.AddRange(queryableInitialResult.ToList());
}
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
Ray
  • 311
  • 2
  • 6
  • 15
  • I could suspect that adding a range has nothing to do here. Does it work when you call `ToList` and just assign it to a variable and then call `AddRange` on that variable? If not, which line of these two fails? – Wiktor Zychla Jan 19 '14 at 22:31
  • It could be the way you are building your search predicate. A StackOverflowException is caused when the call stack gets too big. It is usually caused by recursive calls (e.g. a method calling itself) – Colin Mackay Jan 19 '14 at 23:37
  • AddRange is unlikley to be causing the issue. It is more likely to be the ToList causing the issue. The query is built using your predicate, but the query and predicates are executed when the ToList() is called. I would think there is some recursive call within the predicate or the initial query. – Amleth Jan 19 '14 at 23:43
  • Thanks guys for the suggestions. I tried building predicate using another set of 'categories' (about 400 categories), it didn't throw any error with the same code. I'll double check the 'Categories' bunch that gives me errors. – Ray Jan 20 '14 at 00:53
  • Wiktor is right! the error is actually at 'ToList' call. So any suggestions to cast the iqueryable to a List properly? – Ray Jan 20 '14 at 01:01

0 Answers0