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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:11:00+00:00 2026-05-11T17:11:00+00:00

I’m trying to writing a generic method that will load a record of a

  • 0

I’m trying to writing a generic method that will load a record of a specific type, with a specific ID. Here’s one way that works:

    public abstract class LinqedTable<T> where T : LinqableTable {
    public static T Get(long ID) {
        DataContext context = LinqUtils.GetDataContext<T>();
        var q = from obj in context.GetTable<T>()
                        where obj.ID == ID
                        select obj;
        return q.Single<T>();
    }
}

public abstract class LinqableTable {
    public abstract long ID { get; set; }
}

You can ignore the call to LinqUtils.GetDataContext<T>(); that’s a utility function I’ve got to deal with the fact that I have multiple data contexts in my program. The point is that now I can declare any of my classes as subclasses of LinqableTable, and I can easily instantiate a record of that table just by calling LinqedTable<MyType>.Get(ID).

This has some limitations, however. Firstly, it forces all of my tables to have an I
identity field of type long, named ID. Secondly, because I’m using an abstract method, I am forced to go to the O/R designer and change the inheritance property of every ID field in my system to “override”.

I want more flexibility than that. So naturally, I tried reflection, and came out with the following:

    public abstract class LinqedTable<T> where T : LinqableTable {
    public static T Get(long ID) {
        DataContext context = LinqUtils.GetDataContext<T>();
        var q = from obj in context.GetTable<T>()
                        where obj.IDValue == ID
                        select obj;
        return q.Single<T>();
    }
}

public abstract class LinqableTable {
    internal long IDValue {
        get { return (long)IDProperty.GetValue(this, null); }
        set { IDProperty.SetValue(this, value, null); }
    }
    internal PropertyInfo IDProperty {
        get { return this.GetType().GetProperty(IDPropertyName); }
    }
    internal protected virtual string IDPropertyName {
        get { return "ID"; }
    }
}

Theoretically, this allows me to override the ID column name, the cast to long should be OK with any integral data type, and I don’t need to go defining all my ID columns as overrides.

BUT

Linq doesn’t like this. On the call to q.Single<T>(); I get a runtime error:

The member 'EISS.Utils.LinqableTable.IDValue' has no supported translation to SQL.

OK, today I learned that Linq does some kind of magic on the back end; it doesn’t instantiate obj and just read the IDValue property. So must be there’s some attribute that needs to be set on the IDValue property that lets Linq do its thing.

But what?

  • 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-11T17:11:01+00:00Added an answer on May 11, 2026 at 5:11 pm

    Linq to SQL tries to translate your linq-query into SQL, but it does not know how to translate your property to a column name in the DB.

    A good explanation can be found here on SO:
    simple linq to sql has no supported translation to SQL

    But how to solve it, is another matter. I have with succes used the apporoach from this thread:

    http://social.msdn.microsoft.com/forums/en-US/linqprojectgeneral/thread/df9dba6e-4615-478d-9d8a-9fd80c941ea2/

    Or you can use dynamic query as mentioned here by scott guthrie:

    http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

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

Sidebar

Ask A Question

Stats

  • Questions 97k
  • Answers 97k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer We have chosen to use the Publisher Policy file. The… May 11, 2026 at 7:28 pm
  • Editorial Team
    Editorial Team added an answer I ended up going the OpenXml SDK route - which… May 11, 2026 at 7:28 pm
  • Editorial Team
    Editorial Team added an answer The only real way to get layouts working across browsers… May 11, 2026 at 7:28 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.