Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to remove null value from Lambda expression or Array

How to remove null value from Lambda expression or Array

Scheduled Pinned Locked Moved C#
questionlinqdata-structuresfunctionaltutorial
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 8677026
    wrote on last edited by
    #1

    Here is my code: //Line below shows an email msg obj with To segment being an array (could have null to x number of receipients/emails): emailMessage.EmailParamHeaders.To //Below is creation of mail message obj: MailMessage m = new MailMessage(); //Line blow is adding each To email address to the "m" object emailMessage.EmailParamHeaders.To.ToArray().ToList().ForEach(x => m.To.Add(x)); How can I only add the elements of the array "To" where value is not null? Thank you in advance.

    K 1 Reply Last reply
    0
    • U User 8677026

      Here is my code: //Line below shows an email msg obj with To segment being an array (could have null to x number of receipients/emails): emailMessage.EmailParamHeaders.To //Below is creation of mail message obj: MailMessage m = new MailMessage(); //Line blow is adding each To email address to the "m" object emailMessage.EmailParamHeaders.To.ToArray().ToList().ForEach(x => m.To.Add(x)); How can I only add the elements of the array "To" where value is not null? Thank you in advance.

      K Offline
      K Offline
      kevinnicol
      wrote on last edited by
      #2

      try this

      emailMessage.EmailParamHeaders.To.ToArray().ToList().ForEach(x => {if(m != null) m.To.Add(x);});

      U 1 Reply Last reply
      0
      • K kevinnicol

        try this

        emailMessage.EmailParamHeaders.To.ToArray().ToList().ForEach(x => {if(m != null) m.To.Add(x);});

        U Offline
        U Offline
        User 8677026
        wrote on last edited by
        #3

        Thank you so much for the answer, that worked with minor change: emailMessage.EmailParamHeaders.To.ToArray().ToList().ForEach(x => { if (x != null) m.To.Add(x); });

        P 1 Reply Last reply
        0
        • U User 8677026

          Thank you so much for the answer, that worked with minor change: emailMessage.EmailParamHeaders.To.ToArray().ToList().ForEach(x => { if (x != null) m.To.Add(x); });

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          Why are you converting to array, and then to a list? Also, if your email message supports AddRange, an alternative is to try the following?

          var addresses = (from p in emailMessage.EmailParamHeaders.To
          where p != null
          select p).ToList();
          if (addresses != null && addresses.Count>0)
          m.To.AddRange(addresses);

          Depending what's going on inside the Add method, you can often find that an AddRange works more efficiently.

          *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

          "Mind bleach! Send me mind bleach!" - Nagy Vilmos

          CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups