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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:15:39+00:00 2026-06-02T18:15:39+00:00

I have this functional Linq-to-Sql statement. public IEnumerable<int> GetChildIds(IEnumerable<int> selectedParentIds) { using (var context

  • 0

I have this functional Linq-to-Sql statement.

public IEnumerable<int> GetChildIds(IEnumerable<int> selectedParentIds)
{
    using (var context = new MyContext())
    {
        return context.Children
            .Where(c => selectedParentIds.Contains(c.parentId))
            .Select(c => c.Id)
            .ToList();
    }
}

It gives me the selected child Id’s, as requested but, I’ve been running SQL Profiler.

This statement appears to send a seperate request to the database for each selectedParentId which seems sub-optimal to me.

Is there a way I can restructure this statement to minimise the traffic with the server? Is there a different approach I should take or, is this just as good as it gets?

EDIT

Thanks for the guidance.

This is a mistake on my interpretation of my trace ouput, Linq-To-SQL make a perfectly reasonable statement for SQLServer 2005 just as it does for SQLServer 2008. The problem is elsewhere.

  • 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-02T18:15:42+00:00Added an answer on June 2, 2026 at 6:15 pm

    The same query using LINQ to SQL against SQL 2008 produces a single SQL query. Are you able to test against SQL Server 2008?

    For example:

    IEnumerable<int> list = new List<int>{1,2,3};
    
    Models.Where (m => list.Contains(m.Id)).Select (m => m.Id).ToList();
    

    Produces

    -- Region Parameters
    DECLARE @p0 Int = 1
    DECLARE @p1 Int = 2
    DECLARE @p2 Int = 3
    -- EndRegion
    SELECT [t0].[Id]
    FROM [Models] AS [t0]
    WHERE [t0].[Id] IN (@p0, @p1, @p2)
    

    And the same thing using Entity Framework produces

    SELECT 
    [Extent1].[Id] AS [Id]
    FROM [dbo].[Models] AS [Extent1]
    WHERE [Extent1].[Id] IN (1,2,3)
    

    So without being able to check this against SQL 2005, I’m going to risk it and say, no there’s nothing you can do unless you upgrade the server.

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

Sidebar

Related Questions

I have this code: #include <iostream> #include <functional> struct A { int operator()(int i)
I have this function: public bool IsValidProduct(int productTypeId) { bool isValid = false; if
I am using LINQ to SQL in my project. I have two tables Category
I have this Linq to SQL query sequence that is basically returning a search
I'm having a bit of issue converting the following t-sql statement to Linq (using
I have just finished creating a custom role provider using LINQ to SQL for
I have a query made using linq to SQL which result will be shown
I'm using Linq-To-SQL for a project with around 75 tables. We have to keep
I have this function signature I have to match typedef int (*lua_CFunction) (lua_State *L);//target
I have this function on my controller (Im using CodeIgniter) that reads the database,

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.