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

You start by finding which elements are not already in the collection: var newItems = DownloadedItems. Where(x =>! CurrentCollection.

Any(y => y. Bar == y. Bar)) And then just add them: foreach(var item in newItems) { CurrentCollection.

Add(item); } 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(from x in CurrentCollection select x. Bar); // pick items that have a property bar that doesn't exist yet var newItems = DownloadedItems.

Where(x =>!existingValues. Contains(x. Bar)); // Add them foreach(var item in newItems) { CurrentCollection.

Add(item); }.

You start by finding which elements are not already in the collection: var newItems = DownloadedItems. Where(x =>! CurrentCollection.

Any(y => y. Bar == y. Bar)); And then just add them: foreach(var item in newItems) { CurrentCollection.

Add(item); } 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(from x in CurrentCollection select x.

Bar); // pick items that have a property bar that doesn't exist yet var newItems = DownloadedItems. Where(x =>!existingValues. Contains(x.

Bar)); // Add them foreach(var item in newItems) { CurrentCollection. Add(item); }.

Var newItems = DownloadedItems. Where(i =>! CurrentCollection.

Any(c => c. Attr == i. Attr)).

You could do it like this: CurrentCollection. Any(x => x. Bar == yourGivenValue).

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 => f. Bar == someValue)) { // add item } a more complete solution could be: DownloadedItems.

Where(d =>! CurrentCollection. Any(c => c.

Bar == d. Bar)).ToList() . ForEach(f => CurrentCollection.

Add(f)).

Var newItems = DownloadedItems. Any(y => x. Var existingValues = new HashSet(from x in CurrentCollection select x.

Var newItems = DownloadedItems. Where(x =>!existingValues.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions