467,911 Members | 1,311 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,911 developers. It's quick & easy.

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
  • viewed: 2555
Share:
9 Replies
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by blrmaani | last post: by
10 posts views Thread by Jeff Williams | last post: by
8 posts views Thread by =?Utf-8?B?UGV0ZXI=?= | last post: by
1 post views Thread by =?Utf-8?B?aGVyYmVydA==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.