LINQ Equivalent
-
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<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
-
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<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
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
-
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<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
You must try to find your solution visit to http://techgurulab.com/course/java-tutorials/[^]
-
You must try to find your solution visit to http://techgurulab.com/course/java-tutorials/[^]
http://techgurulab.com/course/asp-net-tutorial/[^] This is link to increase your Programming concept on ASP.NET with C#
-
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<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
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());