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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:32:27+00:00 2026-05-26T03:32:27+00:00

I am using Entity Framework with Linq to Entities, trying to select some data

  • 0

I am using Entity Framework with Linq to Entities, trying to select some data from my database. When I create a Linq query that uses the method IQueryable<int>.Contains, it can only filter the data if I use an external variable! Let me show some example.

This block of code, works perfectly:

var volumes = (from v in work.VolumeAdditiveRepository.All
             where v.AdditivesID == AdditivesID
             select v.MetricID);
var metrics =
    from m in work.MetricRepository.All
    where !volumes.Contains(m.ID)
    select m;

If you take a good look, you can see I use the variable volumes inside this snippet, in the where condition. If I copy the contents of this variable and paste it inside the metrics variable, leading to the code below, it raises the error: "Unable to create a constant value of type 'CalculadoraRFS.Models.Domain.VolumeAditivo'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.".

var metrics =
    from m in work.MetricRepository.All
    where !(from v in work.VolumeAdditiveRepository.All
             where v.AdditivesID == AdditivesID
             select v.MetricID).Contains(m.ID)
    select m;

How can I variable substitution cause such error?! Am I doing (certainly) something wrong?
Thank you!


UPDATE:

Actually, I find out that the Repository Pattern or DbContext seems to be the problem, as @jhamm pointed out. The snippet below doesn’t work either:

var query = from m in work._context.Metric
               where !(from v in work._context.VolumeAdditive
                       where v.AdditivesID == AdditivesID
                       select v.MetricID).Contains(m.ID)
               select m;

But the snippet below works. I just took the context out of the UnitOfWork class, though it is very simply defined there: public CalculadoraRFSContext _context = new CalculadoraRFSContext();.

var _context = new CalculadoraRFSContext();
var query = from m in _context.Metric
               where !(from v in _context.VolumeAdditive
                       where v.AdditivesID == AdditivesID
                       select v.MetricID).Contains(m.ID)
               select m;

Now I’m really confused about this stuff! Wasn’t it supposed to work as expected?!

  • 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-26T03:32:28+00:00Added an answer on May 26, 2026 at 3:32 am

    I used LINQPad to use my EF Database First model on a similar type of query. Both the combined and separate queries gave the same correct results and generated the same SQL. Here is a link on how to use LINQPad with Entity Framework. One difference could be the use of the Repository Pattern, I am not using one. I would recommend testing with the first query to see what SQL is generated. After you run a query, LINQPad has a SQL tab that may help troubleshoot what is going on by looking at the generated SQL.

    If you are still having trouble with the combined LINQ statement, a good next step would be to try the Entity Framework object without the Repository objects. If this query works, there may be something wrong with your Repository objects.

    // Guessing that Metric and VolumeAdditive are the EF Entities
    // LINQPad database dropdown sets the context so they were not set it in these samples
    var metrics =
        from m in Metric
        where !(from v in VolumeAdditive
                 where v.AdditivesID == AdditivesID
                 select v.MetricID).Contains(m.ID)
        select m;
    

    To find out where the issue lies, I would next use the MetricRepository with the VolumeAdditive EF object.

    var metrics =
        from m in work.MetricRepository.All
        where !(from v in VolumeAdditive
                 where v.AdditivesID == AdditivesID
                 select v.MetricID).Contains(m.ID)
        select m;
    

    Then I would switch them to use the Metric EF object with the VolumeAdditiveRepository.

    var metrics =
        from m in Metric
        where !(from v in work.VolumeAdditiveRepository.All
                 where v.AdditivesID == AdditivesID
                 select v.MetricID).Contains(m.ID)
        select m;
    

    Based on the generated SQL and which queries work, I think this should help point you in the right direction. This is based on removing parts of the problem until it works. Then adding them back in until they break to indicate where the issue is. These steps should be done using small incremental changes to minimize the problem space.


    Update:

    Based on the new information, lets try to divide the new information into new questions that we need to answer.

    Maybe the LINQ expression is not able to figure out what to do with the work._context.VolumeAdditive in the where clause. So lets test this theory by using the following. This sets the context to a single variable instead of using work._context.

    var _context = work._context;
    var query = from m in _context.Metric
               where !(from v in _context.VolumeAdditive
                       where v.AdditivesID == AdditivesID
                       select v.MetricID).Contains(m.ID)
               select m;
    

    Maybe using a let statement to define the MetricID’s could resolve this issue.

    var metrics =
        from m in work.MetricRepository.All
        let volumes = from v in work.VolumeAdditiveRepository.All
                 where v.AdditivesID == AdditivesID
                 select v.MetricID
        where !volumes.Contains(m.ID)
        select m;
    

    Based on the results of these tests and mixing and matching the previous 3 tests/questions, we should be getting closer to the answer. When I come up against issues like this, I try to ask my self questions with verifiable answers. Basically, I try to use the Scientific Method to narrow down the issue to find a resolution.

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

Sidebar

Related Questions

I have a problem with the following Linq query using Entity Framework: from o
Using LINQ to Entities with Entity Framework and C#: I have a method that
When using the Entity Framework, does ESQL perform better than Linq to Entities? I'd
I'm new to using LINQ to Entities (or Entity Framework whatever they're calling it)
So, I am using the Linq entity framework. I have 2 entities: Content and
I am using Linq and the Entity Framework. I have a Page Entity, that
I recently switched from using Linq to Sql to the Entity Framework. One of
I switched my DAL from using LINQ over to Entity Framework. Because my application
I'm trying to understand some fundamental best practices using Entity Framework. My EDM design
Supposedly, microsoft says that we should all be using Entity Framework, using LINQ (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.