Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 897283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:51:43+00:00 2026-05-15T14:51:43+00:00

I’m trying to find and run a CompiledQuery given the name. How do I

  • 0

I’m trying to find and run a CompiledQuery given the name. How do I access the compiled query by name and how do I then invoke the delegate?

Here’s as far as I can get – I get the error ‘Error binding to target method’

public class ActivityRepository
{
    private readonly ActivityDataContext _db;

    public ActivityRepository()
    {
        _db = new ActivityDataContext();
    }

    public static Func<ActivityDataContext, int, IQueryable<ProjectObject>>
        GetCompiledLatestProjects = CompiledQuery.Compile
            ((ActivityDataContext db, int projectId) =>
             from c in db.projectObjects
             where c.projectId == projectId
             select c);

    public static Func<ActivityDataContext, Guid, IQueryable<Report>>
        GetCompiledReports = CompiledQuery.Compile
            ((ActivityDataContext db, Guid itemId) =>
             from c in db.Reports
             where c.reportObjectId == itemId
             select c);

// Other compiled queries ommitted, but the results are IQueryable objects that implement a common interface IProjectObject

    delegate IQueryable<IProjectObject> MyDelegate();

    static MyDelegate GetByName(object target, string methodName)
    {
            return (MyDelegate)Delegate.CreateDelegate
                (typeof(MyDelegate), target, methodName);
    }

    public IList<Results> GetResults(string reportName)
    {
            IQueryable<ProjectObject> projectItems = GetLatestProjectObjects(projectId, quantity);
        foreach (projectObject o in projectItems)
        {
            MyDelegate del = GetByName(this, reportName);
             var dbReport = (IProjectObject) GetCompiledReports(_db, o.itemId).FirstOrDefault();
// add results to List and return
         }
     }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:51:44+00:00Added an answer on May 15, 2026 at 2:51 pm

    You can probably do some long winded way of trying to identify any fields of type Func<…> with a DataContext as the first argument that return IQueryable, but you might find it a lot easier to just add a custom attribute:

    [AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
    public class DynamicQueryAttribute : Attribute { }
    

    …which you can then add to your field:

    [DynamicQuery]
    public static Func<ActivityDataContext, int, IQueryable<ProjectObject>>
        GetCompiledLatestProjects = CompiledQuery.Compile
            ((ActivityDataContext db, int projectId) =>
             from c in db.projectObjects
             where c.projectId == projectId
             select c);
    

    You can then select the query based on that, and its name:

    public static IQueryable<IProjectObject> ExecuteQuery(Type ownerType, string name, params object[] args)
    {
        var query = typeof(ownerType)
            .GetFields(BindingFlags.Public | BindingFlags.Static)
            .Where(f =>
                    (f.GetCustomAttributes(typeof(DynamicQueryAttribute), false).Length > 0)
                    && (f.Name.ToLowerInvariant() == name.ToLowerInvariant()))
            .Select(f => (Delegate) f.GetValue(null))
            .SingleOrDefault();
    
        if (query == null)
            return null;
    
        return (IQueryable<IReportObject>)query.DynamicInvoke(args);
    }
    

    Usage:

    var results = ExecuteQuery(
        typeof(ActivityRepository),
        "GetCompiledLatestProjects", 
        dataContext, 
        projectId);
    

    Hope that helps 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.