LINQ tip to use proper for loop

Dear Friends,

As a programmer I would like to share some of the best tips which I found so far and this is one of the tip which I am going to share right away

suppose you have following  piece of code

string[] Names= namecollection.Split(GetDelimeter());

           List<string> NameList = new List<string>();
     

foreach (string name in Names)
          {
              if (!string.IsNullOrEmpty(name ))
              {
                  sb.Append(name .Trim() + ",");
                  NameList .Add(name .Trim());
              }
          }

Now you can write above  for each code  & if condition in following way

foreach (string Nam in Names.Where(Nam=> !string.IsNullOrEmpty(Nam)))
           {
               sb.Append(Nam .Trim() + ",");
             NameList .Add(Nam.Trim());
           }

It is much faster then a earlier code which shown

Hope you like this. Enjoy  Reading

Regards,

Rajat Jaiswal

Advertisements

One thought on “LINQ tip to use proper for loop

  1. Nice post. I wass checking constantly this blog and I’m impressed!
    Extremely useful information specifically the last part 🙂
    I care for sjch info much. I was seeking this particular info
    for a long time.Thank you and gokod luck.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s