473,386 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

List(Of String)

Hello,

How can I filter a List(Of String)?
I need to get the list elements which start with the letters contained
in the variable Text.

Thanks,
Miguel

Oct 9 '07 #1
9 2775
shapper wrote:
Hello,

How can I filter a List(Of String)?
I need to get the list elements which start with the letters contained
in the variable Text.

Thanks,
Miguel
You loop through the items in the list and check the values. There are a
lot of fancy ways of doing it, like using the FindAll method or creating
a filtering enumerator, but they all boil down to looping through all
the items.

If the list is sorted, you can use the BinarySearch method to quickly
locate the items in the list, so that you don't have to loop through the
entire list.

--
Göran Andersson
_____
http://www.guffa.com
Oct 9 '07 #2
Here's both the .net 2.0 example using the predicate delegate and the .net
3.0 example using extension methods.

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

myListOfStrings.Add("Hello");
myListOfStrings.Add("World");
myListOfStrings.Add("Coding");
myListOfStrings.Add("C#");
myListOfStrings.Add("Control");
// ASP.NET 3.5 - Extension Method
foreach (string word in myListOfStrings.Where(i =i.StartsWith("C")))
{
Response.Write(word + "<br/>");
}

Response.Write("<br/>");

// ASP.NET 2.0 - Using the Predicate delegate
foreach (string word in myListOfStrings.FindAll(delegate(string
thisWord) { return thisWord.StartsWith("C") ? true : false; }))
{
Response.Write(word + "<br/>");
}
Output:

Coding
C#
Control

Coding
C#
Control

HTH.

-dl

--
David R. Longnecker
http://blog.tiredstudent.com
Hello,

How can I filter a List(Of String)?
I need to get the list elements which start with the letters contained
in the variable Text.
Thanks,
Miguel

Oct 9 '07 #3
Whoops, just realized you had a VB example. Here's the translation, though...
with VB, you're not gaining a lot with the Extension method.

Dim myListOfStrings As New List(Of String)()

myListOfStrings.Add("Hello")
myListOfStrings.Add("World")
myListOfStrings.Add("Coding")
myListOfStrings.Add("C#")
myListOfStrings.Add("Control")

' ASP.NET 3.5 - Extension Method

For Each word In myListOfStrings.Where(Function(thisWord) thisWord.StartsWith("C"))
Response.Write(word + "<br/>")
Next

Response.Write("<br/>")

' ASP.NET 2.0 - Using the Predicate delegate
For Each word In myListOfStrings.FindAll(Function(thisWord) thisWord.StartsWith("C"))
Response.Write(word + "<br/>")
Next

-dl

--
David R. Longnecker
http://blog.tiredstudent.com
Here's both the .net 2.0 example using the predicate delegate and the
.net 3.0 example using extension methods.

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

myListOfStrings.Add("Hello");
myListOfStrings.Add("World");
myListOfStrings.Add("Coding");
myListOfStrings.Add("C#");
myListOfStrings.Add("Control");
// ASP.NET 3.5 - Extension Method
foreach (string word in myListOfStrings.Where(i =>
i.StartsWith("C")))
{
Response.Write(word + "<br/>");
}
Response.Write("<br/>");

// ASP.NET 2.0 - Using the Predicate delegate
foreach (string word in
myListOfStrings.FindAll(delegate(string
thisWord) { return thisWord.StartsWith("C") ? true : false; }))
{
Response.Write(word + "<br/>");
}
Output:

Coding
C#
Control
Coding
C#
Control
HTH.

-dl

--
David R. Longnecker
http://blog.tiredstudent.com
>Hello,

How can I filter a List(Of String)?
I need to get the list elements which start with the letters
contained
in the variable Text.
Thanks,
Miguel

Oct 9 '07 #4
On Oct 9, 7:27 pm, David R. Longnecker <dlongnec...@community.nospam>
wrote:
Here's both the .net 2.0 example using the predicate delegate and the .net
3.0 example using extension methods.

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

myListOfStrings.Add("Hello");
myListOfStrings.Add("World");
myListOfStrings.Add("Coding");
myListOfStrings.Add("C#");
myListOfStrings.Add("Control");

// ASP.NET 3.5 - Extension Method
foreach (string word in myListOfStrings.Where(i =i.StartsWith("C")))
{
Response.Write(word + "<br/>");
}

Response.Write("<br/>");

// ASP.NET 2.0 - Using the Predicate delegate
foreach (string word in myListOfStrings.FindAll(delegate(string
thisWord) { return thisWord.StartsWith("C") ? true : false; }))
{
Response.Write(word + "<br/>");
}

Output:

Coding
C#
Control

Coding
C#
Control

HTH.

-dl

--
David R. Longneckerhttp://blog.tiredstudent.com
Hello,
How can I filter a List(Of String)?
I need to get the list elements which start with the letters contained
in the variable Text.
Thanks,
Miguel
Hi,

I am having a few problems with your code. I use VB.NET and somehow I
am not being able to convert it.

I need to use predicated in .NET 2.0 but I also need to specify which
letter to start with:
return thisWord.StartsWith(MyFirstLetter)

Anyway, I tried to make this work in VB.NET but no lucky. Could
someone help me out?

This is really strange because I always convert between C# and VB.NET.

Thanks,
Miguel

Oct 9 '07 #5
I thought I corrected and posted some VB examples too... maybe they haven't
hit the servers yet.

Here it is in VB. I extracted it out into a variable... give this a try:

Dim firstLetter As String
firstLetter = "C"
Dim myListOfStrings As New List(Of String)()

myListOfStrings.Add("Hello")
myListOfStrings.Add("World")
myListOfStrings.Add("Coding")
myListOfStrings.Add("C#")
myListOfStrings.Add("Control")

' ASP.NET 2.0 - Using the Predicate delegate
For Each word In myListOfStrings.FindAll(Function(thisWord) thisWord.StartsWith(firstLetter))
Response.Write(word + "<br/>")
Next

HTH.

-dl
--
David R. Longnecker
http://blog.tiredstudent.com
On Oct 9, 7:27 pm, David R. Longnecker <dlongnec...@community.nospam>
wrote:
>Here's both the .net 2.0 example using the predicate delegate and the
.net 3.0 example using extension methods.

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

myListOfStrings.Add("Hello");
myListOfStrings.Add("World");
myListOfStrings.Add("Coding");
myListOfStrings.Add("C#");
myListOfStrings.Add("Control");
// ASP.NET 3.5 - Extension Method
foreach (string word in myListOfStrings.Where(i =>
i.StartsWith("C")))
{
Response.Write(word + "<br/>");
}
Response.Write("<br/>");

// ASP.NET 2.0 - Using the Predicate delegate
foreach (string word in myListOfStrings.FindAll(delegate(string
thisWord) { return thisWord.StartsWith("C") ? true : false; }))
{
Response.Write(word + "<br/>");
}
Output:

Coding
C#
Control
Coding
C#
Control
HTH.

-dl

--
David R. Longneckerhttp://blog.tiredstudent.com
>>Hello,

How can I filter a List(Of String)?
I need to get the list elements which start with the letters
contained
in the variable Text.
Thanks,
Miguel
Hi,

I am having a few problems with your code. I use VB.NET and somehow I
am not being able to convert it.

I need to use predicated in .NET 2.0 but I also need to specify which
letter to start with:
return thisWord.StartsWith(MyFirstLetter)
Anyway, I tried to make this work in VB.NET but no lucky. Could
someone help me out?

This is really strange because I always convert between C# and VB.NET.

Thanks,
Miguel

Oct 9 '07 #6
On Oct 9, 8:05 pm, David R. Longnecker <dlongnec...@community.nospam>
wrote:
I thought I corrected and posted some VB examples too... maybe they haven't
hit the servers yet.

Here it is in VB. I extracted it out into a variable... give this a try:

Dim firstLetter As String
firstLetter = "C"
Dim myListOfStrings As New List(Of String)()

myListOfStrings.Add("Hello")
myListOfStrings.Add("World")
myListOfStrings.Add("Coding")
myListOfStrings.Add("C#")
myListOfStrings.Add("Control")

' ASP.NET 2.0 - Using the Predicate delegate
For Each word In myListOfStrings.FindAll(Function(thisWord) thisWord.StartsWith(firstLetter))
Response.Write(word + "<br/>")
Next

HTH.

-dl

--
David R. Longneckerhttp://blog.tiredstudent.com
On Oct 9, 7:27 pm, David R. Longnecker <dlongnec...@community.nospam>
wrote:
Here's both the .net 2.0 example using the predicate delegate and the
.net 3.0 example using extension methods.
List<stringmyListOfStrings = new List<string>();
myListOfStrings.Add("Hello");
myListOfStrings.Add("World");
myListOfStrings.Add("Coding");
myListOfStrings.Add("C#");
myListOfStrings.Add("Control");
// ASP.NET 3.5 - Extension Method
foreach (string word in myListOfStrings.Where(i =>
i.StartsWith("C")))
{
Response.Write(word + "<br/>");
}
Response.Write("<br/>");
// ASP.NET 2.0 - Using the Predicate delegate
foreach (string word in myListOfStrings.FindAll(delegate(string
thisWord) { return thisWord.StartsWith("C") ? true : false; }))
{
Response.Write(word + "<br/>");
}
Output:
Coding
C#
Control
Coding
C#
Control
HTH.
-dl
--
David R. Longneckerhttp://blog.tiredstudent.com
Hello,
>How can I filter a List(Of String)?
I need to get the list elements which start with the letters
contained
in the variable Text.
Thanks,
Miguel
Hi,
I am having a few problems with your code. I use VB.NET and somehow I
am not being able to convert it.
I need to use predicated in .NET 2.0 but I also need to specify which
letter to start with:
return thisWord.StartsWith(MyFirstLetter)
Anyway, I tried to make this work in VB.NET but no lucky. Could
someone help me out?
This is really strange because I always convert between C# and VB.NET.
Thanks,
Miguel
Hi,

I am getting an error in:
For Each word In myListOfStrings.FindAll(Function(thisWord)
thisWord.StartsWith(firstLetter))

on word Function. It says expression expected. I don't know why.

I was trying something like:
categories.FindAll(AddressOf FindAllByPrefix)

Where FindAllByPrefix is a function that checks if the word starts
with that prefix.
The problem is that in this case I am not able to pass the PrefixValue
to the function.

I was following this example:
http://msdn2.microsoft.com/en-us/library/6sh2ey19.aspx

Any idea?

Of course I would use a simple loop but I am trying to make this work
with Predicate.

Thanks,
Miguel
Oct 9 '07 #7
Okay, apparently the issue lies in that I'm using VB9.0 targeting .NET 2.0...
or something...

According to this article, anonymous delegates are not available in VB until
2008, but he does a good job of explaining how to wrap the functionality
into a quick method.

http://www.paulstovell.net/blog/inde...ual-basic-net/

-dl

--
David R. Longnecker
http://blog.tiredstudent.com
On Oct 9, 8:05 pm, David R. Longnecker <dlongnec...@community.nospam>
wrote:
>I thought I corrected and posted some VB examples too... maybe they
haven't hit the servers yet.

Here it is in VB. I extracted it out into a variable... give this a
try:

Dim firstLetter As String
firstLetter = "C"
Dim myListOfStrings As New List(Of String)()
myListOfStrings.Add("Hello")
myListOfStrings.Add("World")
myListOfStrings.Add("Coding")
myListOfStrings.Add("C#")
myListOfStrings.Add("Control")
' ASP.NET 2.0 - Using the Predicate delegate
For Each word In myListOfStrings.FindAll(Function(thisWord)
thisWord.StartsWith(firstLetter))
Response.Write(word + "<br/>")
Next
HTH.

-dl

--
David R. Longneckerhttp://blog.tiredstudent.com
>>On Oct 9, 7:27 pm, David R. Longnecker
<dlongnec...@community.nospamwrote:

Here's both the .net 2.0 example using the predicate delegate and
the .net 3.0 example using extension methods.

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

myListOfStrings.Add("Hello");
myListOfStrings.Add("World");
myListOfStrings.Add("Coding");
myListOfStrings.Add("C#");
myListOfStrings.Add("Control");
// ASP.NET 3.5 - Extension Method
foreach (string word in myListOfStrings.Where(i =>
i.StartsWith("C")))
{
Response.Write(word + "<br/>");
}
Response.Write("<br/>");
// ASP.NET 2.0 - Using the Predicate delegate
foreach (string word in myListOfStrings.FindAll(delegate(string
thisWord) { return thisWord.StartsWith("C") ? true : false; }))
{
Response.Write(word + "<br/>");
}
Output:
Coding
C#
Control
Coding
C#
Control
HTH.
-dl

--
David R. Longneckerhttp://blog.tiredstudent.com
Hello,
>
How can I filter a List(Of String)?
I need to get the list elements which start with the letters
contained
in the variable Text.
Thanks,
Miguel
Hi,

I am having a few problems with your code. I use VB.NET and somehow
I am not being able to convert it.

I need to use predicated in .NET 2.0 but I also need to specify
which
letter to start with:
return thisWord.StartsWith(MyFirstLetter)
Anyway, I tried to make this work in VB.NET but no lucky. Could
someone help me out?
This is really strange because I always convert between C# and
VB.NET.

Thanks,
Miguel
Hi,

I am getting an error in:
For Each word In myListOfStrings.FindAll(Function(thisWord)
thisWord.StartsWith(firstLetter))
on word Function. It says expression expected. I don't know why.

I was trying something like:
categories.FindAll(AddressOf FindAllByPrefix)
Where FindAllByPrefix is a function that checks if the word starts
with that prefix.
The problem is that in this case I am not able to pass the PrefixValue
to the function.
I was following this example:
http://msdn2.microsoft.com/en-us/library/6sh2ey19.aspx
Any idea?

Of course I would use a simple loop but I am trying to make this work
with Predicate.

Thanks,
Miguel

Oct 9 '07 #8
On Oct 9, 8:50 pm, David R. Longnecker <dlongnec...@community.nospam>
wrote:
Okay, apparently the issue lies in that I'm using VB9.0 targeting .NET 2.0...
or something...

According to this article, anonymous delegates are not available in VB until
2008, but he does a good job of explaining how to wrap the functionality
into a quick method.

http://www.paulstovell.net/blog/inde...ous-methods-in...

-dl

--
David R. Longneckerhttp://blog.tiredstudent.com
On Oct 9, 8:05 pm, David R. Longnecker <dlongnec...@community.nospam>
wrote:
I thought I corrected and posted some VB examples too... maybe they
haven't hit the servers yet.
Here it is in VB. I extracted it out into a variable... give this a
try:
Dim firstLetter As String
firstLetter = "C"
Dim myListOfStrings As New List(Of String)()
myListOfStrings.Add("Hello")
myListOfStrings.Add("World")
myListOfStrings.Add("Coding")
myListOfStrings.Add("C#")
myListOfStrings.Add("Control")
' ASP.NET 2.0 - Using the Predicate delegate
For Each word In myListOfStrings.FindAll(Function(thisWord)
thisWord.StartsWith(firstLetter))
Response.Write(word + "<br/>")
Next
HTH.
-dl
--
David R. Longneckerhttp://blog.tiredstudent.com
On Oct 9, 7:27 pm, David R. Longnecker
<dlongnec...@community.nospamwrote:
>>Here's both the .net 2.0 example using the predicate delegate and
the .net 3.0 example using extension methods.
>>List<stringmyListOfStrings = new List<string>();
>>myListOfStrings.Add("Hello");
myListOfStrings.Add("World");
myListOfStrings.Add("Coding");
myListOfStrings.Add("C#");
myListOfStrings.Add("Control");
// ASP.NET 3.5 - Extension Method
foreach (string word in myListOfStrings.Where(i =>
i.StartsWith("C")))
{
Response.Write(word + "<br/>");
}
Response.Write("<br/>");
// ASP.NET 2.0 - Using the Predicate delegate
foreach (string word in myListOfStrings.FindAll(delegate(string
thisWord) { return thisWord.StartsWith("C") ? true : false; }))
{
Response.Write(word + "<br/>");
}
Output:
Coding
C#
Control
Coding
C#
Control
HTH.
-dl
>>--
David R. Longneckerhttp://blog.tiredstudent.com
Hello,
>>>How can I filter a List(Of String)?
I need to get the list elements which start with the letters
contained
in the variable Text.
Thanks,
Miguel
Hi,
>I am having a few problems with your code. I use VB.NET and somehow
I am not being able to convert it.
>I need to use predicated in .NET 2.0 but I also need to specify
which
letter to start with:
return thisWord.StartsWith(MyFirstLetter)
Anyway, I tried to make this work in VB.NET but no lucky. Could
someone help me out?
This is really strange because I always convert between C# and
VB.NET.
>Thanks,
Miguel
Hi,
I am getting an error in:
For Each word In myListOfStrings.FindAll(Function(thisWord)
thisWord.StartsWith(firstLetter))
on word Function. It says expression expected. I don't know why.
I was trying something like:
categories.FindAll(AddressOf FindAllByPrefix)
Where FindAllByPrefix is a function that checks if the word starts
with that prefix.
The problem is that in this case I am not able to pass the PrefixValue
to the function.
I was following this example:
http://msdn2.microsoft.com/en-us/library/6sh2ey19.aspx
Any idea?
Of course I would use a simple loop but I am trying to make this work
with Predicate.
Thanks,
Miguel
Great article. I was looking Goggling but I couldn't find anything
like that.

Thanks!
Miguel

Oct 9 '07 #9
check out
http://coders-lab.dnn-portal.com/Def...px?tabid=16942

for alot of options in generics.

Since you don't have anonymous delegates in vbnet, you might use the
article, so see how you do static ones for filtering (and/or sorting)
"shapper" <md*****@gmail.comwrote in message
news:11*********************@k79g2000hse.googlegro ups.com...
Hello,

How can I filter a List(Of String)?
I need to get the list elements which start with the letters contained
in the variable Text.

Thanks,
Miguel

Oct 9 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Klaus Neuner | last post by:
Hello, I need a function that converts a list into a set of regexes. Like so: string_list = print string_list2regexes(string_list) This should return something like:
4
by: blrmaani | last post by:
Here is what I want: string s1 = "This is a list of string"; list<string> s2 = s1.some_method(); Now, I should be able to traverse list s2 and get each member ( which is of type 'string' ). ...
9
by: incredible | last post by:
how to sort link list of string
10
by: Jeff Williams | last post by:
How can I get a list of the Groups both Local and Domain groups a User belongs to.
6
by: Bryan | last post by:
I am writing a class that has a list of strings as one of its properties. I want to be able to run some code whenever that list of strings is modified through the class property. Here is the...
8
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
I'm trying to get a list of SQL Server Instances thru a VB.NET application. I have tried GetDataSource and SMO. I have also tried using ListSQLSvr.exe from...
7
by: Karch | last post by:
I need to find the fastest way in terms of storage and searching to determine if a given string contains one of a member of a list of strings. So, think of it in terms of this: I have a string such...
10
by: lpinho | last post by:
Hi all, I have a class (named for the example myObject) that can be of several types (int, string, float, etc), instead of using a object to define it's type I used a generic. public class...
1
by: =?Utf-8?B?aGVyYmVydA==?= | last post by:
I use the following code to programmatically bind a DataGridView and a BindingNavigator to various List(of myClass). This works fine with each 'myClass' offering public properties. Dim mAllUsers...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.