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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:31:13+00:00 2026-05-27T18:31:13+00:00

I have two database tables: ‘MyTable’ which has a standard auto-incrementing primary key integer

  • 0

I have two database tables:

  • ‘MyTable’ which has a standard auto-incrementing primary key integer field.
  • ‘UserAccess’ table is a mapping table that contains links to all systems via a composite key join.

MyTable:

ID              int    
IsDeleted       bit    
Email           varchar(255)   

UserAccess:

UserID          guid    
ProviderID      int    
ProviderUserID  varchar(255)   

Both tables are set up in my application via Entity Framework 4.1 Code First.

The primary table (my system/provider) has its own ID (e.g. 42). It can find if a user has access by finding if they have a record in the table for the ProviderID of 42.

My problem comes when trying to find out who a particular record in MyTable belongs to by mapping the ProviderUserID (varchar) to the primary key of my table (int). I can’t change the ProviderUserID as there are some systems that use strings as their primary keys (URLs / emails / etc).

This problem can be solved with SQL simply:

SELECT * FROM MyTable AS M
LEFT OUTER JOIN UserAccess AS U ON U.ProviderID = 42 AND M.ID = U.ProviderUserID

But in Entity Framework and more specifically, a combination of LINQ to Entities and SQL Compact Edition – I’m unable to do type casting at runtime.

The obvious candidates like Convert.ToString() and Convert.ToInt() don’t work as it’s L2E. And the SqlFunctions.Convert isn’t available as this is SQL CE.

This problem probably goes away if I go off SQL CE and can use the SqlFunctions, but as CE is quite useful for quickly building a working DbContext for Unit Testing with – I was less excited about doing that – though it’s a possibility if I can’t find a way around this.

I’ve tried a number of ways, and got close with different failings each time. The one below would work great if both types (UserAccess.ProviderUserID and MyTable.ID) were of the same type – but they aren’t 🙂

var data = (from m in DB.MyTable
            let userID = DB.UserAccess.Where(x => x.ProviderID == 42).FirstOrDefault(x => p.ID == x.ProviderUserID).UserID
            select new DomainModel()
            {
               ID = m.ID
               Email = m.Email,
               IsDeleted = m.IsDeleted,
               UserID =userID,
            });

Any thoughts on where I’m going wrong or what I can do to battle this?

  • 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-27T18:31:14+00:00Added an answer on May 27, 2026 at 6:31 pm

    While this is not the ideal solution, it’s the one I’ve settled on for the time being.

    • I’ve changed the database used by the unit testing to a SQL Express database and that enabled me to use the SqlFunctions class and the methods in it.

    I was then able to tweak the query to look similar to the following:

    var data = (from m in DB.MyTable
                join i in DB.UserAccess.Where(x => x.ProviderID == 42) on SqlFunctions.StringConvert((double)m.ID).Trim() equals i.ProviderUserID into useraccess
                from ua in useraccess.DefaultIfEmpty()
                select new DomainModel()
                {
                   ID = m.ID
                   Email = m.Email,
                   IsDeleted = m.IsDeleted,
                   UserID = ua.UserID,
                });
    

    I’ll keep an eye on this question and if there are any better answers I’ll happy upvote / change my accepted answer.

    p.s. as an aside, the .Trim() was added as the initial string conversion was adding buckets full of whitespace to the start of the string.

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

Sidebar

Related Questions

I have two database tables with the following structure: actions: action_id int(11) primary key
I have two database tables, 'Lists' and 'Notes'. Lists has columns _id, listname Notes
I have a database two tables and a linking table that I need a
I have two tables in my database, called ratings and movies . Ratings: |
I have two tables in my database: NEWS table with columns: id - the
I have two tables in my database schema that represent an entity having a
I have two tables in my database. Comments CommentsID MembersID CommentsBy CommentsDesc CommentsActive CommentsDateEntered
We have two tables in a SQL Server 2005 database, A and B.There is
I have two simple tables in my database. A card table that contains Id,
I have two related tables in my database: Page and Tag. One Page can

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.