I was asked “Why I never used LINQ?” I guess the real answer was, I never thought about it. I really had no good reason for not using LINQ other it was new and unfamiliar. When I finally used LINQ; I found I was getting rid of old habits and making code simpler and cleaner.
Example of code written in C#:
foreach (Charges charge in (ary))
{
if (charge.ChargeDescription.ToLower().Contains("total"))
{
ary.Remove(charge);
}
}Code converted to LINQ:
var rslt = from Charges charge in ary where charge.ChargeDescription.ToLower().Contains("total") select charge;
while(rslt.Count() > 0)
{
Charges chargese = rslt.ElementAt(0);
ary.RemoveAt(ary.IndexOf(Chargese));
}
To this day I still use C# with a little LINQ to simplify my code. I use LINQ for validations, quick look up and data manipulations. It is definitely a change from what I used to do, but worth learning. What I like about the above code is I had eliminated the nested if without complicating the code.