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

  • SEARCH
  • Home
  • 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 222741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:09:20+00:00 2026-05-11T19:09:20+00:00

If I have 2 query sources how do I find ones that are in

  • 0

If I have 2 query sources how do I find ones that are in one that are not in the other?

example of join to find items in both:

var results = from item1 in qs1.Items
   join item2 in qs2 on item1.field1 equals item2.field2
   select item1;

So what would the linq code be to return the items in qs1 that are not in qs2?

  • 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-11T19:09:20+00:00Added an answer on May 11, 2026 at 7:09 pm

    Darren Kopp’s answer:

    var excluded = items1.Except(items2);
    

    is the best solution from a performance perspective.

    (NB: This true for at least regular LINQ, perhaps LINQ to SQL changes things as per Marco Russo’s blog post. However, I’d imagine that in the “worst case” Darren Kopp’s method will return at least the speed of Russo’s method even in a LINQ to SQL environment).

    As a quick example try this in LINQPad:

    void Main()
    {
       Random rand = new Random();
       int n = 100000;
       var randomSeq = Enumerable.Repeat(0, n).Select(i => rand.Next());
       var randomFilter = Enumerable.Repeat(0, n).Select(i => rand.Next());
    
       /* Method 1: Bramha Ghosh's/Marco Russo's method */
       (from el1 in randomSeq where !(from el2 in randomFilter select el2).Contains(el1) select el1).Dump("Result");
    
       /* Method 2: Darren Kopp's method */
       randomSeq.Except(randomFilter).Dump("Result");
    }
    

    Try commenting one of the two methods out at a time and try out the performance for different values of n.

    My experience (on my Core 2 Duo Laptop) seems to suggest:

    n = 100. Method 1 takes about 0.05 seconds, Method 2 takes about 0.05 seconds
    n = 1,000. Method 1 takes about 0.6 seconds, Method 2 takes about 0.4 seconds
    n = 10,000. Method 1 takes about 2.5 seconds, Method 2 takes about 0.425 seconds
    n = 100,000. Method 1 takes about 20 seconds, Method 2 takes about 0.45 seconds
    n = 1,000,000. Method 1 takes about 3 minutes 25 seconds, Method 2 takes about 1.3 seconds
    

    Method 2 (Darren Kopp’s answer) is clearly faster.

    The speed decrease for Method 2 for larger n is most likely due to the creation of the random data (feel free to put in a DateTime diff to confirm this) whereas Method 1 clearly has algorithmic complexity issues (and just by looking you can see it is at least O(N^2) as for each number in the first collection it is comparing against the entire second collection).

    Conclusion: Use Darren Kopp’s answer of LINQ’s ‘Except’ method

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

Sidebar

Related Questions

I have a number of different data sources that I need to query and
I have a jquery calendar widget that do query several event sources on the
Surprisingly can't find a match for my question. I have one table that I
I am using in C# MYsql .I have query that works if I run
I've noticed that a few Wordpress blogs have query statistics present in their footer
I have a query that is dynamically built after looking up a field list
I have a query that originally looks like this: select c.Id, c.Name, c.CountryCode, c.CustomerNumber,
I have a query that looks like this: public IList<Post> FetchLatestOrders(int pageIndex, int recordCount)
I have a big fat query that's written dynamically to integrate some data. Basically
I find that I am using a lot of join queries, especially to get

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.