none
Scary exception when constructing a complex query for Linq to SQL RRS feed

  • Question

  • Can anybody help me to identify the source of this error and whether there might be something I can do to address it? I have a feeling that I'm hitting the limits of the complexity allowed for LINQ to SQL queries since I ran into the error below.

    "A generic instantiation for type 'System.Func`2' from assembly 'System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' was too deep.  Is this generic type defined or used recursively?"

    This is a complicated case but basically I'm doing a Union style query between values that are gathered by joining completely separate tables in the source database and creating a projection which maintains all possible columns by creating simple concrete types which mimic the underlying tables which I refer to as "placeholders". These placeholder classes are identical to their automatically generated LINQ to SQL counterparts except for each field that is not nullable in the corresponding table is instead nullable in the placeholder class. The projection then contains only a few placeholder references in raw form so that it can be reused to formulate more queries. All of this placeholder nonsense is a necessary workaround because LINQ to SQL does not allow full tables to be null or missing within the projection when doing a Union or Concat as discussed here .

    All of this was working until I just kept adding more placeholders to my projection as necessary for the implementation of a specific query and then I started getting the error listed above at the point in the code when the query is declared and assigned to a variable (not executed yet I believe).

    Other important characteristics of my query is that it has over 100 let statements in order to work around a different LINQ to SQL shortcomming with how early optimization by the Query Provider breaks UNIONs and the query is recomposed 3 to 4 times after the initial union to modify the projection and to add additional filtering via the where clause.

    Related Topics:

    Full Exception Details:

    System.TypeLoadException occurred
      Message="A generic instantiation for type 'System.Func`2' from assembly 'System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' was too deep.  Is this generic type defined or used recursively?"
      Source="DataAccess"
      TypeName="System.Func`2"
      StackTrace:
           at DataAccess.ServiceAssistantDataContext.get_AccountTransactions()
           at Queries.MyQueries.GetUnionResults() in C:\linqtest\LinqConversion\Queries\MyUnionQueries.cs:line 18
      InnerException:
    Thursday, September 10, 2009 4:44 PM

Answers

  • Ok, I've identified this on my own after some trial and error. It appears that there is a limit of 98 let statements within a given LINQ to SQL query.

    var query = from s in Servcd
    let a000 = 0
    let a001 = 0
    let a002 = 0
    let a003 = 0
    let a004 = 0
    let a005 = 0
    let a006 = 0
    let a007 = 0
    let a008 = 0
    let a009 = 0
    
    let a010 = 0
    let a011 = 0
    let a012 = 0
    let a013 = 0
    let a014 = 0
    let a015 = 0
    let a016 = 0
    let a017 = 0
    let a018 = 0
    let a019 = 0
    
    let a020 = 0
    let a021 = 0
    let a022 = 0
    let a023 = 0
    let a024 = 0
    let a025 = 0
    let a026 = 0
    let a027 = 0
    let a028 = 0
    let a029 = 0
    
    let a030 = 0
    let a031 = 0
    let a032 = 0
    let a033 = 0
    let a034 = 0
    let a035 = 0
    let a036 = 0
    let a037 = 0
    let a038 = 0
    let a039 = 0
    
    let a040 = 0
    let a041 = 0
    let a042 = 0
    let a043 = 0
    let a044 = 0
    let a045 = 0
    let a046 = 0
    let a047 = 0
    let a048 = 0
    let a049 = 0
    
    let a050 = 0
    let a051 = 0
    let a052 = 0
    let a053 = 0
    let a054 = 0
    let a055 = 0
    let a056 = 0
    let a057 = 0
    let a058 = 0
    let a059 = 0
    
    let a060 = 0
    let a061 = 0
    let a062 = 0
    let a063 = 0
    let a064 = 0
    let a065 = 0
    let a066 = 0
    let a067 = 0
    let a068 = 0
    let a069 = 0
    
    let a070 = 0
    let a071 = 0
    let a072 = 0
    let a073 = 0
    let a074 = 0
    let a075 = 0
    let a076 = 0
    let a077 = 0
    let a078 = 0
    let a079 = 0
    
    let a080 = 0
    let a081 = 0
    let a082 = 0
    let a083 = 0
    let a084 = 0
    let a085 = 0
    let a086 = 0
    let a087 = 0
    let a088 = 0
    let a089 = 0
    
    let a090 = 0
    let a091 = 0
    let a092 = 0
    let a093 = 0
    let a094 = 0
    let a095 = 0
    let a096 = 0
    let a097 = 0
    //let a098 = 0 // Uncomment this line to see the error
    
    select s;
    
    Also on a related note, SQL Server supposedly limits nesting to 32 levels although I haven't tested this yet personally.
    • Marked as answer by kainhart Thursday, September 10, 2009 7:41 PM
    Thursday, September 10, 2009 7:40 PM