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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:07:16+00:00 2026-05-23T06:07:16+00:00

I have two generic lists of type T. Both lists contain same type, and

  • 0

I have two generic lists of type T. Both lists contain same type, and I’d like to create a third list (or a filtered version of list 2) based on the items in list two that do not exist in List 1, based on the ID of each item.

Each list holds a “Package” object, which has an ID property.

Right now I mocked up the code using For Each loops, which I know is horrible (the Big O is constant time) so I’d like a more efficent method.

this code is in VB per project requirments, but I prefer C# – so either code sample would work for me.

Private Sub RemoveStockPackagesFromSelection()

    Dim p As Package
    Dim packageList As List(Of Package) = New List(Of Package)
    Dim stockPackageList As List(Of Package) = New List(Of Package)
    Dim result As List(Of Package) = New List(Of Package)

    ' Fill list with User's Packages
    For i As Integer = 0 To ListBox2.Items.Count - 1
        p = New Package
        p.Id = CInt(ListBox2.Items(i).Value)
        p.Name = ListBox2.Items(i).Text
        packageList.Add(p)
    Next

    ' Fill list with Stock Packages to compare:
    Dim ds As DataSet = DAL.GetStandardPackages()

    For Each dr As DataRow In ds.Tables(0).Rows
        p = New Package
        p.Id = CInt(dr.Item("id"))
        stockPackageList.Add(p)
    Next

    ' Do Compare and Filter
    For Each p1 As Package In packageList
        For Each p2 As Package In stockPackageList
            If Not p1.Id = p2.Id Then
                result.Add(p2)
            End If
        Next
    Next

    ' Here is our new trimmed list:
    Response.Write(result.Count)

End Sub

What is a nice and clean LINQ or Lamda way of doing this filtering? What is the Big O of my method and what would be the Big O of the proposed method (just to satify my curiosity).

Thanks

  • 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-23T06:07:17+00:00Added an answer on May 23, 2026 at 6:07 am

    LINQ Except Method

    This would be the cleanest way, as suggested by Maxim and svick but requires an overridden Equals method which equals on ID or you have to provide a comparer (see svicks answer).

    var result = stockPackageList.Except(packageList).ToList();
    

    Resources
    Many LINQ samles can be found in the msdn at http://msdn.microsoft.com/en-us/vcsharp/aa336746


    I’ll leave the initial parts of my answer for reference:

    Brute force way:

    var result = stockPackageList
                  .Where(x => packageList.All(package => x.Id != package.Id))
                  .ToList();
    

    should do the trick. You just have to translate the lambda syntax to vb.net.

    This query will filter all items from stockPackageList which IDs are not present in all items of packageList.

    You may invert the query:

    var result = stockPackageList
                  .Where(x => packageList.Any(package => x.Id == package.Id) == false)
                  .ToList();
    

    The Any query will return true if any item in packageList has a matching id. This query should run a little faster because it has not to traverse the whole collection, as All has to.

    Using Eqality:

    If your package object implements IEquatable<Package> you can shorten the code down to

    var result = stockPackageList
                  .Where(x => packageList.Contains(x) == false)
                  .ToList();
    

    Using a Hash Set:

    If you want to use a hash set you can do

    var hash = new HashSet<string>(packageList.Select(x=>x.Id));
    var result = stockPackageList.Where(x => hash.Contains(x.Id) == false).ToList();
    

    This saves computation time when the lists become large, as faester and Ivan Danilov pointed out.

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

Sidebar

Related Questions

I have two Generic Lists with the same objects Type T within them. For
I have two classes, CheckboxItemsList which extends a generic list, and CheckboxItems , which
I have 3 different classes, a generic entity class, and then two classes that
Is it possible to use Except() for two List's that have two different classes
I have two same project in which I have two classes under same namespace
i have, (in Linq), an IEnumerable of type Client. Now i have to return
I have this code that works. public class HelloWorldController : Controller { UAStagingEntities db
Has anyone had any luck model binding two or more collections using the code
I have followed the suggestions from this post to try and get Distinct() working
I'm trying to implement something quite simple but I'm on my first steps in

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.