Add items to a collection if the collection does NOT already contain it by comparing a property of the items?

Basically, how do I make it so I can do something similar to: CurrentCollection.Contains(...), except by comparing if the item's property is already in the collection?

public class Foo
    public Int32 bar;

ICollection<Foo> CurrentCollection;
ICollection<Foo> DownloadedItems;

//LINQ: Add any downloaded items where the bar is not already in the collection?

You start by finding which elements are not already in the collection:

var newItems = DownloadedItems.Where(x => !CurrentCollection.Any(y => ==;

And then just add them:

foreach(var item in newItems)

Note that the first operation may have quadratic complexity if the size of DownloadedItems is close to the size of CurrentCollection. If that ends up causing problems (measure first!), you can use a HashSet to bring the complexity down to linear:

// collect all existing values of the property bar
var existingValues = new HashSet<Foo>(from x in CurrentCollection select;
// pick items that have a property bar that doesn't exist yet
var newItems = DownloadedItems.Where(x => !existingValues.Contains(;
// Add them
foreach(var item in newItems)

how to check if object already exists in a list, If the above are not true for your situation, just use Any() : If you are populating the list with many items, performance will suffer with myList. the equals method to set up what properties of your object have to match Collections. I'd create a list from it when the Adding was done, otherwise, just use the  I want to find all items in one collection that do not match another collection. The collections are not of the same type, though; I want to write a lambda expression to specify equality. A LINQPad example of what I'm trying to do:

Using R.Martinho Fernandes method and converting to 1 line:

CurrentCollection.AddRange(DownloadedItems.Where(x => !CurrentCollection.Any(y =>;

Collections (C#), If your collection contains elements of only one data type, you can use When you retrieve an element from a generic collection, you do not have to Add("​sockeye"); // Iterate through the list. foreach (var salmon in salmons) { Console. the Item[TKey] property of Dictionary to quickly find an item by key. I am learning programming and my problem is that I have a bunch of objects and I want to add these objects to a list only if the list does not already contain that object. Secondly if the object is already contained I want to ignore that object and add the next one instead. I think I have got the first part working just need some help with part

You can use Enumerable.Except:

It will compare the two lists and return elements that appear only in the first list.


List<T>.Exists(Predicate<T>) Method (System.Collections.Generic), true if the List<T> contains one or more elements that match the conditions defined by the specified List<Part> parts = new List<Part>(); // Add parts to the list. parts. Contains("seat"))); // Check if an item with Id 1444 exists. In C# and Visual Basic, it is not necessary to create the Predicate<string> delegate ( Predicate(Of  Notice that the VBA Collection is not typed in any way as we didn’t have to declare what types of objects it stores. This is because a VBA Collection stores object of type Variant. By default the Add procedure will push items to the end of the VBA Collection.

You can call the Any method and pass a value to compare to whatever property of the type of object in the collection

if (!CurrentCollection.Any(f => == someValue))
    // add item

a more complete solution could be:

DownloadedItems.Where(d => !CurrentCollection.Any(c => ==
    .ForEach(f => CurrentCollection.Add(f));

New Trends in Conceptual Representation: Challenges To Piaget's Theory, That is why one cannot readily decide which collection contains more members. If the child only acted as if each class were a collection, the child would lose much. It is a It might appear that the same counting forward strategy used to add 2 to 8 Intension does not refer to sets of objects but to sets of properties, and the  ConcurrentDictionary<TKey,TValue> is designed for multithreaded scenarios. You do not have to use locks in your code to add or remove items from the collection. However, it is always possible for one thread to retrieve a value, and another thread to immediately update the collection by giving the same key a new value.

Or using All

    .AddRange(DownloadedItems.Where(x => CurrentCollection.All(y => !=;

The Web Collection Revealed Standard Edition: Adobe Dreamweaver , May not be copied, scanned, or duplicated, in whole or in part. clicking OK to close the warning box that asks if you want to also remove the WithThe Sea Spa menu item selected in the Property inspector, click the Add menu item button above the first Save your work, click OK, then compare your screen to Figure 18. The items do not have to be of the same type but they normally are. The VBA Sheets collection can contain both worksheets and chart sheets. A collection makes it easy to perform the same task on multiple items e.g. print all the values. Collections are similar to arrays as they both store groups of similar items.

System.Collections.Generic.List<T> Class, Capacity property explicitly. item: The item to add to the end of the list. If System.Collections.Generic.List<T>.Count already equals System. Comparing null with any reference type is allowed and does not generate an exception If the list does not contain the specified value, the method returns a negative integer. {note} Unlike most other collection methods, forget does not return a new modified collection; it modifies the collection it is called on. forPage() The forPage method returns a new collection containing the items that would be present on a given page number. The method accepts the page number as its first argument and the number of items to

.NET 2.0 for Delphi Programmers, For example, you might want a case-insensitive comparison, or you might the Hashtablethrough the Item property, which allows you to use array-like syntax removesasinglekey/valuepair, without raising an exception if the key does not exist. IDictionary synonym, Contains) tests whether the hash already 'knows about'  This property can only be set in Extensible Application Markup Language (XAML) via the collection syntax shown, or by accessing the collection object and using its various methods such as Add. The property to access the collection object itself is read-only, and the collection itself is read-write.

The Ultimate Guide To Collections in Excel VBA, If you have used the language for any length of time then you will have used Let us now look at a second example where we don't know the number of items in Declare Dim coll As New Collection ' Add item - VBA looks after resizing coll. So if you are not dealing with a larger number of items then using a Collection​  The BindingContext property inherited by Control has a key-part in data binding. With this property, you can get a CurrencyManager, which you can use to implement complex data binding. This property changes every time you add your control to a Form, Panel, or something inherited from Control.

  • Using the hash-map method made my method 5x faster.
  • I think this is the best practice