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. LINQ
  4. LINQ Equivalent

LINQ Equivalent

Scheduled Pinned Locked Moved LINQ
csharpdatabaselinqtutorial
5 Posts 4 Posters 5 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());

    <pre lang="text">

    using (StreamReader sr = new StreamReader(filepath)) { IEnumerable&lt;string&gt; 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

    R R U 3 Replies 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());

      <pre lang="text">

      using (StreamReader sr = new StreamReader(filepath)) { IEnumerable&lt;string&gt; 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

      R Offline
      R Offline
      Richard Deeming
      wrote on last edited by
      #2

      Something like this should do the trick:

      string[] items = File.ReadLines(filepath)
      .Where(line => !string.IsNullOrEmpty(line))
      .Select(line => line.Split(','))
      .Select(element => element[0].Trim())
      .ToArray();

      cboSiteLocation.Items.AddRange(items);


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      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());

        <pre lang="text">

        using (StreamReader sr = new StreamReader(filepath)) { IEnumerable&lt;string&gt; 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

        R Offline
        R Offline
        Raushank03
        wrote on last edited by
        #3

        You must try to find your solution visit to http://techgurulab.com/course/java-tutorials/[^]

        R 1 Reply Last reply
        0
        • R Raushank03

          You must try to find your solution visit to http://techgurulab.com/course/java-tutorials/[^]

          R Offline
          R Offline
          Raushank03
          wrote on last edited by
          #4

          http://techgurulab.com/course/asp-net-tutorial/[^] This is link to increase your Programming concept on ASP.NET with C#

          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());

            <pre lang="text">

            using (StreamReader sr = new StreamReader(filepath)) { IEnumerable&lt;string&gt; 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

            U Offline
            U Offline
            User 11678350
            wrote on last edited by
            #5

            Below is my answer despite it is too late:

            var lines = File.ReadLines(filepath).ToArray();

            (
            from line in lines
            where !string.IsNullOrEmpty(line)
            select line.Split(',')[0]
            ).ToList()
            .ForEach(item => this.cboSiteLocation.Items.Add(item.Trim());

            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