Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can probably do some long winded way of trying to identify any fields of type Func&lt;...> with a DataContext as the first argument that return IQueryable, but you might find it a lot easier to just add a custom attribute:</p> <pre><code>[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)] public class DynamicQueryAttribute : Attribute { } </code></pre> <p>...which you can then add to your field:</p> <pre><code>[DynamicQuery] public static Func&lt;ActivityDataContext, int, IQueryable&lt;ProjectObject&gt;&gt; GetCompiledLatestProjects = CompiledQuery.Compile ((ActivityDataContext db, int projectId) =&gt; from c in db.projectObjects where c.projectId == projectId select c); </code></pre> <p>You can then select the query based on that, and its name:</p> <pre><code>public static IQueryable&lt;IProjectObject&gt; ExecuteQuery(Type ownerType, string name, params object[] args) { var query = typeof(ownerType) .GetFields(BindingFlags.Public | BindingFlags.Static) .Where(f =&gt; (f.GetCustomAttributes(typeof(DynamicQueryAttribute), false).Length &gt; 0) &amp;&amp; (f.Name.ToLowerInvariant() == name.ToLowerInvariant())) .Select(f =&gt; (Delegate) f.GetValue(null)) .SingleOrDefault(); if (query == null) return null; return (IQueryable&lt;IReportObject&gt;)query.DynamicInvoke(args); } </code></pre> <p>Usage:</p> <pre><code>var results = ExecuteQuery( typeof(ActivityRepository), "GetCompiledLatestProjects", dataContext, projectId); </code></pre> <p>Hope that helps :)</p>
 

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