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

  • SEARCH
  • Home
  • 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 7497949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:17:03+00:00 2026-05-29T19:17:03+00:00

I am trying to write some generic linqtoSQL which performs operation on entities based

  • 0

I am trying to write some generic linqtoSQL which performs operation on entities based on their primary keys. Some entities I am working with have composite primary keys.

Fundamentally I want to be able to do things such as:

if( PrimaryKey(foo) == PrimaryKey(bar) ) ...

or,

from oldEntity in oldTableOrCollection
join newEntity in newTableOrCollection
on PrimaryKeyOf(oldEntity) equals PrimaryKeyOf(newEntity)
select new {oldEntity, newEntity}

with only the requirement that the entities have primary keys.

Here’s what I’ve found so far:

It’s possible to do things such as

var query = from row in oneTableOfRows
            join otherRow in anotherTableOfRows on
            new { row.Column1, row.Column2 }
            equals
            new { otherRow.Column1, otherRow.Column2}
            select ...;

in which linqtosql will use the anonymous types for comparison (provided the property names match).

I can then abstract the method for selecting the columns on which to join to allow generic row types:

void DoQuery<RowType,KeyType>(Table<RowType> oneTableOfRows,
                         Table<RowType> anotherTableOfRows,
                         Func<RowType, KeyType> keySelector)
{
    var query = from row in oneTableOfRow
            join otherRow in anotherTableOfRows on
            keySelector(row)
            equals
            keySelector(otherRow)
            select ...;
}

...

Func<rowType, keyType> myKeySelector = row => new { row.Column1, row.Column2 };

DoQuery(table, otherTable, myKeySelector);

What I’m trying to move to now, is where the keySelector will select the primary key of any RowType. I am using a custom template based on http://www.codeplex.com/l2st4 to generate my entities (which itself is fairly similar to the default in VS).
I’m hoping to ideally give each generated RowType the ability to select its primary key as gathered from the dbml. So far the ouptut looks like the following:

public interface IPrimaryKeyEntity<PrimaryKeyType>
{
    PrimaryKeyType PrimaryKey { get; }
}

//Here, Product is a table with composite primary key based on its ProductID and it's ProductPriceVersionID columns

//I've tried using both class and struct as the primary key type for entities
public class ProductPrimaryKey
{
    public Guid ProductID;
    public Guid ProductPriceVersionID;
}

public partial class Product : IPrimaryKeyEntity<ProductPrimaryKey>,
                               INotifyPropertyChanging, INotifyPropertyChanged
{
    #region IPrimaryKeyEntity Implementation
public ProductPrimaryKey PrimaryKey
    {
        get
        { return new ProductPrimaryKey()
                     {
                         ProductID = this.ProductID,
                         ProductPriceVersionID = this.ProductPriceVersionID
                     };
        }
    }
#endregion

    ...
    //Rest is mostly standard LinqToSql entity generation
}

Going back to the original aim, I can now compile the following for all my primary key entities:

from oldEntity in oldTableOrCollection
join newEntity in newTableOrCollection
on oldEntity.PrimaryKey equals newEntity.PrimaryKey
select new {oldEntity, newEntity}

However, runtime, I get the infamous [PrimaryKey] “has no supported translation to SQL” exception.

I understand that to translate to SQL it is necessary to use Expression‘s however I am pretty unfamiliar with Linq.Expressions and have had no progress yet trying to apply it to my situation…

If anyone can improve on what I’ve got so far or has a better method in general I’d be grateful to know….

Cheers

  • 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-29T19:17:06+00:00Added an answer on May 29, 2026 at 7:17 pm

    The problem is that you are calling a function here:

    join … on oldEntity.PrimaryKey equals newEntity.PrimaryKey

    A property-get is a function call. Linq to SQL does not know this function so it cannot translate it.

    The only way to make this work is to build an expression that corresponds to this SQL:

            join otherRow in anotherTableOfRows on
            new { row.Column1, row.Column2 }
            equals
            new { otherRow.Column1, otherRow.Column2}
    

    There is no other way, I can assure you. You can do it like this: First, you declare a custom tuple class:

    class CustomTuple2<T1,T2>
    {
     public T1 Item1 { get; set; }
     public T2 Item2 { get; set; }
    }
    

    You do this for all possible member counts (for example 8 or 16).

    Let’s look at how a join gets translated:

    IQueryable<T1> t1 = ...; //table 1
    IQueryable<T2> t2 = ...; //table 2
    var joined = t1.Join(t2, _t1 => _t1.Key, _t2 => _t2.Key);
    

    The lambda parameters are expressions. You need to build these two expressions using expression trees. This is the magic!

    For example, the first lambda:

    var param = Expression.Parameter(typeof(T1), "_t1");
    var key = Expression.Property(param, "Key");
    var lambda1 = Expression.Lambda(param, key);
    

    Do the same for the second lambda. (Remember, that this was pseudo-code. I don’t exactly recall how everything was called.)

    Finally, invoke the Join:

    var joined = typeof(Queryable).GetMethod("Join").Invoke(t1, t2, lambda1, lambda2);
    

    So that is how you build a join at runtime! Please note, that all of this was pseudo-code. It will take you some research to figure the APIs out and make it work, but you can surely do it.

    Also, if you have a composite key, you will need the mentioned tuple class to project the key members into.

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

Sidebar

Related Questions

I am trying to write some generic LINQ queries for my entities, but am
I'm trying to write some reasonably generic networking code. I have several kinds of
I'm trying to write some code to find a specific XmlNode object based on
I am working with the classes in the System.Windows.Documents namespace, trying to write some
I am trying to write a generic library in pure c , just some
I'm trying to write some documentation and can't think of a generic name for
I am trying to write a generic comparison routine. However, some of the items
I'm trying to write a function which re-uses the implicit conversions which I have
I'm trying to write some generic http response handler functions that sometimes open UIAlertViews.
I'm trying to write some generic structures. Essentially, what I need for my purpose

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.