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,
Miguelshapper wrote:
Quote:
Originally Posted by
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.
--
Gran Andersson
_____
http://www.guffa.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
Quote:
Originally Posted by
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
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
Quote:
Originally Posted by
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
Quote:
Originally Posted by
>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
On Oct 9, 7:27 pm, David R. Longnecker <dlongnec...@.community.nospam>
wrote:
Quote:
Originally Posted by
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
>
Quote:
Originally Posted by
Hello,
>
Quote:
Originally Posted by
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
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
Quote:
Originally Posted by
On Oct 9, 7:27 pm, David R. Longnecker <dlongnec...@.community.nospam>
wrote:
>
Quote:
Originally Posted by
>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
Quote:
Originally Posted by
>>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
On Oct 9, 8:05 pm, David R. Longnecker <dlongnec...@.community.nospam>
wrote:
Quote:
Originally Posted by
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
>
Quote:
Originally Posted by
On Oct 9, 7:27 pm, David R. Longnecker <dlongnec...@.community.nospam>
wrote:
>
Quote:
Originally Posted by
Quote:
Originally Posted by
Here's both the .net 2.0 example using the predicate delegate and the
.net 3.0 example using extension methods.
>
Quote:
Originally Posted by
Quote:
Originally Posted by
List<stringmyListOfStrings = new List<string>();
>
Quote:
Originally Posted by
Quote:
Originally Posted by
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/>");
>
Quote:
Originally Posted by
Quote:
Originally Posted by
// 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:
>
Quote:
Originally Posted by
Quote:
Originally Posted by
Coding
C#
Control
Coding
C#
Control
HTH.
>
Quote:
Originally Posted by
Quote:
Originally Posted by
-dl
>
Quote:
Originally Posted by
Quote:
Originally Posted by
--
David R. Longneckerhttp://blog.tiredstudent.com
>Hello,
>
Quote:
Originally Posted by
Quote:
Originally Posted by
>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,
>
Quote:
Originally Posted by
I am having a few problems with your code. I use VB.NET and somehow I
am not being able to convert it.
>
Quote:
Originally Posted by
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?
>
Quote:
Originally Posted by
This is really strange because I always convert between C# and VB.NET.
>
Quote:
Originally Posted by
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
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/ind...sual-basic-net/
-dl
--
David R. Longnecker
http://blog.tiredstudent.com
Quote:
Originally Posted by
On Oct 9, 8:05 pm, David R. Longnecker <dlongnec...@.community.nospam>
wrote:
>
Quote:
Originally Posted by
>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
Quote:
Originally Posted by
>>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
List(Of String)
check out
http://coders-lab.dnn-portal.com/De...spx?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" <mdmoura@.gmail.comwrote in message
news:1191951464.062111.77670@.k79g2000hse.googlegro ups.com...
Quote:
Originally Posted by
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
>
0 comments:
Post a Comment