Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# more efficient way of comparing two collections

Tags:

c#

.net

c#-4.0

I have two collections

List<Car> currentCars = GetCurrentCars();
List<Car> newCars = GetNewCars();

I don't want to use foreach loop or something because i think there should be much better way of doing this.

I am looking for more efficient way to compare this collections and to get results:

  1. List of cars which are in newCars and not in currentCars
  2. List of cars which are not in newCars and in currentCars

Type Car has int property Id.

There was an answer, which is already deleted saying What i mean by saying efficient: less code, less mechanics, and more readable cases

So thinking this way what is the cases i have?

What would be less code, less mechanics, and more readable cases?

like image 350
Joper Avatar asked Jul 13 '11 14:07

Joper


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

You can do it like this:

// 1) List of cars in newCars and not in currentCars
var newButNotCurrentCars = newCars.Except(currentCars);

// 2) List of cars in currentCars and not in newCars
var currentButNotNewCars = currentCars.Except(newCars);

The code uses the Enumerable.Except extension method (available in .Net 3.5 and over).

I believe this fulfills your criteria of "less code, less mechanics, and more readable".

like image 59
Doctor Jones Avatar answered Sep 20 '22 22:09

Doctor Jones


You can use Except:

var currentCarsNotInNewCars = currentCars.Except(newCars);
var newCarsNotInCurrentCars = newCars.Except(currentCars);

But this has no performance benefit over the foreach solution. It just looks cleaner.
Also, be aware of the fact, that you need to implement IEquatable<T> for your Car class, so the comparison is done on the ID and not on the reference.

Performancewise, a better approach would be to not use a List<T> but a Dictionary<TKey, TValue> with the ID as the key:

var currentCarsDictionary = currentCars.ToDictionary(x => x.ID);
var newCarsDictionary = newCars.ToDictionary(x => x.ID);

var currentCarsNotInNewCars = 
    currentCarsDictionary.Where(x => !newCarsDictionary.ContainsKey(x.Key))
                         .Select(x => x.Value);

var newCarsNotInCurrentCars = 
    newCarsDictionary.Where(x => !currentCarsDictionary.ContainsKey(x.Key))
                     .Select(x => x.Value);
like image 30
Daniel Hilgarth Avatar answered Sep 20 '22 22:09

Daniel Hilgarth