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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. LINQ version equivalent

LINQ version equivalent

Scheduled Pinned Locked Moved C#
csharpdatabaselinqtutorialannouncement
4 Posts 2 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.
  • M Offline
    M Offline
    Member 7278598
    wrote on last edited by
    #1

    I am trying to get my head around LINQ and am going through some old code to practice, below is some code that i want to convert but cannot work out how to do it.
    anyone have any ideas please.
    Reading a csv file, getting first element only with some blank lines and should be able to populate combobox with something like:
    cboSiteLocation.Items.AddRange(lines.ToArray());

    using (StreamReader sr = new StreamReader(filepath))
    {
    IEnumerable<string> lines = File.ReadLines(filepath); // LINQ query here

                    string\[\] element;
    
                    foreach (string line in lines)
                    {
                        if (!string.IsNullOrEmpty(line))
                        {
                            element = line.Split(',');
                            this.cboSiteLocation.Items.Add(element\[0\].Trim());
                        }
                    }                    
                }
    

    Thanks in advance

    P 1 Reply Last reply
    0
    • M Member 7278598

      I am trying to get my head around LINQ and am going through some old code to practice, below is some code that i want to convert but cannot work out how to do it.
      anyone have any ideas please.
      Reading a csv file, getting first element only with some blank lines and should be able to populate combobox with something like:
      cboSiteLocation.Items.AddRange(lines.ToArray());

      using (StreamReader sr = new StreamReader(filepath))
      {
      IEnumerable<string> lines = File.ReadLines(filepath); // LINQ query here

                      string\[\] element;
      
                      foreach (string line in lines)
                      {
                          if (!string.IsNullOrEmpty(line))
                          {
                              element = line.Split(',');
                              this.cboSiteLocation.Items.Add(element\[0\].Trim());
                          }
                      }                    
                  }
      

      Thanks in advance

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

      Are you looking for something like this perhaps?

      IEnumerable<string> output = File.ReadAllLines(@"C:\Course\socket.io\example1Copy\views\index.jade")
      .Where(x => !string.IsNullOrWhiteSpace(x))
      .Select(x => x.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault());

      M 1 Reply Last reply
      0
      • P Pete OHanlon

        Are you looking for something like this perhaps?

        IEnumerable<string> output = File.ReadAllLines(@"C:\Course\socket.io\example1Copy\views\index.jade")
        .Where(x => !string.IsNullOrWhiteSpace(x))
        .Select(x => x.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault());

        M Offline
        M Offline
        Member 7278598
        wrote on last edited by
        #3

        Yes thank you, exactly what i wanted. ;)

        P 1 Reply Last reply
        0
        • M Member 7278598

          Yes thank you, exactly what i wanted. ;)

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

          You are welcome. Glad to help.

          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