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 112159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:30:03+00:00 2026-05-11T02:30:03+00:00

I have multiple tables with an id component. I’d like to use LINQ to

  • 0

I have multiple tables with an ‘id’ component. I’d like to use LINQ to get an item from one of these tables with the correct LINQ-To-SQL type, but only using one CompiledQuery.

Here’s an example that works currently. Say the object type defined in the DBML is ‘myitem’ and its table is ‘myitems’.

    var getOneItem = CompiledQuery.Compile(         (MyDataContext db, long id) => ((from i in db.myitems                                               where i.id == id                                               select i).SingleOrDefault())); 

I’d like to make this, where the type can be ‘myitem’, ‘myitems2’, or whatever table and object type I might be interested in.

    var getOneItemAnyTable = CompiledQuery.Compile(         (Table tab, Type T long id) => ((from anItem in tab<T>                                               where anItem.id == id                                               select anItem).SingleOrDefault())); 

I made up this syntax because I’m not sure what the syntax should look like here. Any ideas if it’s possible? Would I have to munge the DBML to make some kind of superclass for the types involved?

Thank you.

  • 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. 2026-05-11T02:30:03+00:00Added an answer on May 11, 2026 at 2:30 am

    I’m pretty sure you can’t make the one compiled-query span multiple tables (if that is what you mean); how would it know where to look? EF supports something like this with inheritance (and multiple tables), but even with inheritance LINQ-to-SQL only supports single-table discriminated inheritance.

    The heart of this question is largely identical to this one from earlier today. The tricky bit is the primary key, since that is hard to define abstractly. The rest is just GetTable<T> (with LINQ-to-SQL at least).

    You don’t need a superclass in this case – just a way to get the primary key. Fortunately, I seem to recall that LINQ-to-SQL offers a way to do this (without needing to use the attributes, which is not a pre-req of LINQ-to-SQL); the following doesn’t pre-compile (and is untested), but should be fairly close:

    (update fixed and tested)

    Usage first:

    Foo foo = ctx.Get<Foo>(fooid); Bar bar = ctx.Get<Bar>(barid); 

    code:

        public static TItem Get<TItem, TKey>(         this DataContext ctx, TKey key)         where TItem : class     {         var table = ctx.GetTable<TItem>();         var primaryKey = ctx.Mapping.GetMetaType(typeof(TItem))                 .DataMembers.Where(             member => member.IsPrimaryKey).Single().Member.Name;          var item = Expression.Parameter(typeof(TItem), 'item');         var lambda = Expression.Lambda<Func<TItem, bool>>(             Expression.Equal(                 Expression.PropertyOrField(item, primaryKey),                 Expression.Constant(key, typeof(TKey))),                 item);         return table.Single(lambda);     }     public static TItem Get<TItem>( // common case         this DataContext ctx, int key)         where TItem : class     {         return Get<TItem, int>(ctx, key);     }     public static TItem Get<TItem>( // common case        this DataContext ctx, string key)        where TItem : class     {         return Get<TItem, string>(ctx, key);     } 

    I’ll see if I can find a way to pre-compile it…

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

Sidebar

Related Questions

I want to have generalised email templates. Currently I have multiple email templates with
I have a script that appends some rows to a table. One of the
I have a snippet to create a 'Like' button for our news site: <iframe
I have a project that adds elements to an AutoCad drawing. I noticed that
I have a new web app that is packaged as a WAR as part
I have several USB mass storage flash drives connected to a Ubuntu Linux computer
I have found this example on StackOverflow: var people = new List<Person> { new
I have a login.jsp page which contains a login form. Once logged in the
i have a input tag which is non editable, but some times i need
There doesn't seem to be any tried and true set of best practices to

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.