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:
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?
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 ...
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? 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.
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.
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".
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With