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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:06:54+00:00 2026-05-24T23:06:54+00:00

When one creates a linq query as: var ids = new List<int> { 1,

  • 0

When one creates a linq query as:

var ids = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var q = from r in context.Table
        where ids.Contains(r.Id)
        select r;

for use with EF, it generates the T-SQL similar to

SELECT ... FROM Table
    WHERE Id IN ( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 )

In general this is what I expect, though it does seem to mess up the ‘Query plan compilation’ in that each query is different, and thus needs to be compiled before being executed.

Since EF is otherwise very good at using variables ((@p_linq_) to avoid this, it does not (by default) do this for these sort of queries.

Is it at all possible to avoid this?

Table-Valued parameters spring to mind, but that is not yet supported.

btw: in real life the queries are a lot more complicated and the number of items in the ids list is a lot longer, but still needed to filter out the data. We do not want to filter on the ‘client’.

  • 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-24T23:06:56+00:00Added an answer on May 24, 2026 at 11:06 pm

    I implemented this once by modifying the TSQL generated by EF to emit an xml string parameter which was evaluated as a table valued parameter in the query itself.

    The impact of this wasn’t as grand as I had hoped and while performance improved vastly in some areas, other queries that previously performed very well, now performed terribly.

    Ultimately, we ended up switching the implementation to an opt-in approach by having a custom Contains method that was mapped as a model defined function (http://blogs.msdn.com/b/efdesign/archive/2009/01/07/model-defined-functions.aspx). So the usage was like

        var ids = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        var q = from r in context.Table
        where CriteriaHelpers.Contains(ids, r.Id)
        select r;
    

    and this generated an something like the following sql (which avoided the query plan recompiling each time).

    SELECT ... FROM Table
    WHERE Id IN (SELECT Entity.ID.value('.', 'Id') AS ID FROM @parameterName.nodes('/items/i') AS Entity(ID))
    

    This approach also has the added benefit of getting around the 2100 parameter limitation…

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

Sidebar

Related Questions

I have a Linq to Entities query like this one: var results = from
I'm having trouble building an Entity Framework LINQ query whose select clause contains method
I am struggling pulling a simple list of records from one table object and
I have a LINQ query which creates an anonymous collection. One thing I'm returning
Ok, I have one JavaScript that creates rows in a table like this: function
I have a LINQ query that uses 1 table + a large number of
I'm new to LINQ, and I have trouble with a query I'm trying to
I use LINQ to query my MSSQL 2005 database. I want to show several
I'm new to Linq-to-SQL and I don't what is the correct way to use
I have a Linq query that returns an anonymous type, which I then use

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.