C# – LINQ, ForEach, Classes, Override

Some 2.5 years ago I have first written an article for LINQ, when I was learning it. Now, I was watching at some videos about algorithms and I realized I have not used LINQ in quite a while and I have forgotten how it works. Thus, I have decided to make my Sunday evening a little more productive and to write a small program, that can remind me of what I was the LINQ usage 🙂cs

Thus, lets imagine that we have 6 companies and each one has a name, CEO, profit and country. In order to mimic this idea, I have built up the class Company.cs:

As you see above, I have overridden the ToString method with my own, which I would be using later. Something else worth noting, is that I have optional parameters in the builder and I have setter only for the CompanyCEO name. The rest is standard.

In my other class, called LinqApp.cs I define 6 objects of type Company. Then I use LINQ to build up first the Bulgarian companies only and then only the once with profit above 0. This is what I get:


This is the code from the LinqApp.cs class:

If you want to see the files in GitHub, here they are! Enjoy! 😀



Tagged with: , ,