Linq to Entities - StackOverflow when looking for large amount of items RRS feed

  • Question

  • Sorry this was posted twice.  First time it was in Linq to SQL.

    I'm using .NET 4.0, SQL Server 2008, C#.

    When I have many itemIDs to search for in a List, I'm getting a stack overflow.

    So, my scenerio is like the following:

    List<int> itemids = newList<int>();

    // itemids get populated with 450 integers which are Primary Keys.

    IQueryable<MCQ> l = (from r in context.MyEntity where (itemids.Contains(r.ItemInstance.Item_ID))
         select r);  

    // Then when I attempt to get a count I get a stackOverflow

    TotalCount = l.Count();

    Is there another way to do this?


    Friday, August 17, 2012 1:15 PM

All replies

  • Hi,

    There are a few different ways to get a count of the objects you are after, so yes there is another way. But I wanted to try and understand your situation a little better before I talked about them.

    Which version of EF is this that you are using? That will determine some of the options that you have for doing this query differently.

    What is the ItemInstance property that you are checking?

    How many rows are there in the table that this is querying, just so I have an idea when I am trying to re-create it.

    I assume that the count method is just to test out the query, since the expected result will be 450 correct?

    A quick attempt to reproduce this on my machine showed that it works in a simple scenario, it timed out when I made the itemIds list really large but no stack overflow. So I am clearly missing something.


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    Friday, August 17, 2012 4:50 PM
  • I'm using EF 4.0

    ItemInstance is a EF navigation property.

    There are 6,000 rows in the table.  In Production there could be several hundred thousand.

    This snippet of code is a part of a much larger method.  The count is actually for paging logic which i'm not showing here.

    Essentially the code i'm posting is reduced down to the barest essentials.  This Linq-to-Entities will get translated to tSQL as something like:

    select * from myTable

    where MyKey in 1,2,3,4,5.....

    I picked 450 becuase that's what the User was reporting to me.  When I originally wrote this code I was using Lists of up to 30 which works.  Larger Lists seem to break it.


    Monday, August 20, 2012 1:41 PM