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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:01:45+00:00 2026-05-24T00:01:45+00:00

Is there an alternative to using .Contains() to select objects in Entity Framework that

  • 0

Is there an alternative to using .Contains() to select objects in Entity Framework that exist in a specified list? Contains() works great if your list is small, however once you start getting a few thousands items the performance is terrible.

return (from item in context.Accounts
        where accountIdList.Contains(item.AccountId)
        select item).ToList();

I’m using EF 4.0, .Net Framework 4.0, and SQL Server 2005. I’m not opposed to a SQL solution either since the query that EF generates only takes a second to run on SQL for about 10k items.

  • 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-24T00:01:45+00:00Added an answer on May 24, 2026 at 12:01 am

    I found an alternative that runs in about a second using a SQL Stored Procedure and a comma-delimited string for the parameter. Much better than the 5+ minutes EF was taking using .Contains()

    It is run from my code using the following:

    string commaDelmitedList = string.Join(",", accountIdList);
    return context.GetAccountsByList(commaDelmitedList).ToList();
    

    The StoredProcedure (simplified) looks like this:

    SELECT *
    FROM   Accounts as T1 WITH (NOLOCK)
    INNER JOIN (
        SELECT Num FROM dbo.StringToNumSet(@commaDelimitedAccountIds, ',')
    ) as [T2] ON [T1].[AccountId] = [T2].[num]
    

    And the User-Defined function dbo.StringToNumSet() looks like this:

    CREATE FUNCTION [dbo].[StringToNumSet] (
     @TargetString  varchar(MAX),
     @SearchChar varchar(1)
     )
    RETURNS @Set TABLE (
     num int not null
     )
    AS
    BEGIN
     DECLARE @SearchCharPos  int,  @LastSearchCharPos int
     SET @SearchCharPos = 0
     WHILE 1=1
     BEGIN
      SET @LastSearchCharPos = @SearchCharPos
      SET @SearchCharPos = CHARINDEX( @SearchChar, @TargetString, @SearchCharPos + 1 )
      IF @SearchCharPos = 0
      BEGIN
       INSERT @Set( num ) VALUES ( SUBSTRING( @TargetString, @LastSearchCharPos + 1, DATALENGTH( @TargetString ) ) )
       BREAK
      END
      ELSE
       INSERT @Set( num ) VALUES ( SUBSTRING( @TargetString, @LastSearchCharPos + 1, @SearchCharPos - @LastSearchCharPos - 1 ) )
     END
     RETURN
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When communicating concurrency conflicts to your application layer, is there an alternative to using
I'm using System.IO.Stream.Read(byte[] buffer, int offset, int count) . Is there an alternative to
Is there any alternative image manipulation library for .net? I would prefer something that
Is there an alternative ActionScript 3 lightweight framework out there similar to Flex, but
Is there an alternative version of std::find_if that returns an iterator over all found
Is there any way to get a List<string> which contains all 'usings' within a
I need to validate a string that contains XML Data, there is no schema
I have been using extplorer http://extplorer.sourceforge.net/ Are there any good alternatives which run on
is there an alternative for mysql_insert_id() php function for PostgreSQL? Most of the frameworks
Is there any alternative for WPF (windows presentation foundation) in python? http://msdn.microsoft.com/en-us/library/aa970268.aspx#Programming_with_WPF

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.