Note that there are some explanatory texts on larger screens.

plurals
  1. POHow Can I Use a Method Returning IQueryable<T> in Compiled Linq to SQL Query Without Error
    primarykey
    data
    text
    <p>I have a method returning an IQueryable that I use in a compiled query. This method prevents me from having to incorporate a very large linq to sql query repeatedly while modifying just the WHERE clause. For example, internally we reference these items using a DB-generated identity column so I have one function that takes that the internal id value and returns the object searching on the internal id column. For our web services, we get a different id and I have another function that uses the same underlying query with only the different where clause (or singleordefault, etc.). All of this works great until I try to use this as a compiled query (which is essential in this case for performance). When I invoke the compiled query, I receive "The query contains references to items defined on a different data context." with a gigantic stack trace. Note that this only happens for a certain sequence of execution when this is running on our application server. If I set up a simple test case that just calls this method it works fine.</p> <p>My question is, how can I get this working under NET 4.5? I realize that I was probably "lucky" in NET 4 that this worked since I was using the version of my function that references an instance variable for the datacontext in my dataaccess class. Here's the original version that worked under 4.0:</p> <pre><code>public IQueryable&lt;Log&gt; GetBaseLogQuery(EncDataContext context) { var query = from pl in context.DbLog ... (lots of joins and lets) select new Log { LogID = pl.LogId, UniqueID = pl.UniqueID, ... many other memebers set } ; return query; } public IQueryable&lt;Log&gt; GetBaseLogQuery() { return GetBaseQuery(Context); // Context is an instance variable } private static Func&lt;EncDataContext, int, string, Log&gt; _loadLogByUniqueId; public Log LoadLog(int organizationId, string uniqueId) { if (_loadLogByUniqueId == null) { var query = GetBaseLogQuery(); _loadLogByUniqueId = CompiledQuery.Compile&lt;EncDataContext, int, string, Log&gt;( (context, orgId, uid) =&gt; query.SingleOrDefault(pl =&gt; pl.OrganizationId == orgId &amp;&amp; pl.UniqueID == uid) ); } var ret = _loadLogByUniqueId.Invoke(Context, organizationId, uniqueId); return ret; } </code></pre> <p>If I try to use the method and pass it the datacontext directly in the compile like this:</p> <pre><code>_loadLogByUniqueId = CompiledQuery.Compile&lt;EncDataContext, int, string, Log&gt;( (context, orgId, uid) =&gt; GetBaseLogQuery(context).SingleOrDefault(pl =&gt; pl.OrganizationId == orgId &amp;&amp; pl.UniqueID == uid) </code></pre> <p>I get a different error -- "Member access 'Int32 LogID' of 'BusinessEntities.Log' not legal on type 'System.Linq.IQueryable`1[BusinessEntities.Log]."</p> <p>So after some research people mention needing to expand the method before compile time so that it can be translated into SQL by linq to sql. However, I'm not able to get this working. When I try to assign the output of GetBaseLogQuery to an expression so that I can call the LinqKit invoke on it, I get a type mismatch.</p> <p>Is there any way for me to not have to duplicate the linq to sql query in GetBaseLogQuery in every compiled query I currently use it in?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload