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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:43:47+00:00 2026-06-12T16:43:47+00:00

I’m writing a query using Linq to Entities and Entity Framework. My app uses

  • 0

I’m writing a query using Linq to Entities and Entity Framework. My app uses two databases. Cross database is not an option due to client demands.

Lets say MyEntity resides in first database (so it is on one EF Model) and it references an entity in the second database SecondDBEntity (so it is on another EF Model), which has a composite key. Now, lets say I want to retrieve all MyEntity which references a set of SecondDBEntity (In our example, entities with key [1, 1], [1, 2] and [1, 3]).

The SQL I want to generate is this:

SELECT *
  FROM MyEntity
 WHERE (MyEntity.ForeignKeyOne = 1 AND MyEntity.ForeignKeyTwo = 1)
    OR (MyEntity.ForeignKeyOne = 1 AND MyEntity.ForeignKeyTwo = 2)
    OR (MyEntity.ForeignKeyOne = 1 AND MyEntity.ForeignKeyTwo = 3)

I have then tried the following code:

var setOfEntitiesToSearch = LetsAssumeThisIsAnIEnumerableOfSecondDBEntity;
return (from myEntity in DataContext.MyEntityList
       where setOfEntitiesToSearch.Any(entityToSearch => entityToSearch.KeyOne == myEntity.ForeignKeyOne && entityToSearch.KeyTwo == myEntity.ForeignKeyTwo)
      select myEntity).ToList();

This code compiles fine, but when I execute, it gives me the error:

"Unable to create a constant value of type 'SecondDBEntity'. Only primitive types or enumeration types are supported in this context."

My main problem is because entities are in different databases. I’m probably doing something silly, because this is a farely common query I’m trying to build. So, I believe I’m missing some feature in EF which will allow me to create this query. Maybe some CompositeKey structure? Or some way to mix those EF Models?

Thanks in advance.

  • 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-06-12T16:43:48+00:00Added an answer on June 12, 2026 at 4:43 pm

    Linq can’t translate the setOfEntitiesToSearch into a sql query. It can only use primative types. Note, an IEnumerable is not the same as an enum. Linq can use an enum because it can be translated to a value of type integer, but it can’t translate an IEnumerable into a primative type.

    From what I can think of, you could go about this two different ways. (Note, I’m typing this code without compiling, so it may need debugged.)

    1) Loop through setOfEntitiesToSearch and build a List like this:

    List<MyEntity> results = new List<MyEntity>();
    foreach (item in setOfEntitiesToSearch) {
        results.AddRange(
            from myEntity in DataContext.MyEntityList           
            where myEntity.ForeignKeyOne == item.KeyOne && myEntity.ForeignKeyTwo == item.KeyTwo
            select myEntity).ToList()
        );
    }
    return results;
    

    2) Get all DataContext.MyEntityList into an in-memory list and query it.

    var entityList = DataContext.MyEntityList.ToList()
    return entityList.Where(myEntity => setOfEntitiesToSearch.Any(entityToSearch => entityToSearch.KeyOne == myEntity.ForeignKeyOne && entityToSearch.KeyTwo == myEntity.ForeignKeyTwo)).ToList()
    

    Drawback to #1 – calling the db in a loop.

    Drawback to #2 – entityList may be very large depending on the number of items DataContext.MyEntityList returns.

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

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I am writing an app with both english and french support. The app requests
I am using Paperclip to handle profile photo uploads in my app. They upload
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.