Springen naar inhoud


- - - - -
VS 2012

Linq Eerste Poging

Visual Basic 2012

  • Log in a.u.b. om te beantwoorden
Er zijn 3 reacties in dit onderwerp

#1 ludo64

ludo64

    Rookie Developer

  • Leden
  • Pip
  • 30 berichten
    Laatst bezocht 25 jul 2019 15:42
  • LocatieTongeren

Geplaatst op 30 juni 2019 - 10:55

Hallo,
Ik heb een eerste poging gedaan met Linq (waarvan ik eigenlijk nog niets gelezen heb).
Van 50 Amerikaanse staten (listbox1), moet ik degene die 4 of meer klinkers bevatten, in listbox2 zetten.
Het zal waarschijnlijk weer iets onnozel zijn, maar dit is de code:
Visual Basic Code:
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
'Haal ze er uit, of nog beter stop ze er nooit in:
Dim states As New List(Of String)(
{
"Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut",
"Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas",
"Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi",
"Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York",
"North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island",
"South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington",
"West Virginia", "Wisconsin", "Wyoming"
})
ListBox1.DataSource = states

Dim vowels = states.Where(Function© "aeiouAEIOU".Contains©)
Dim nrOfVowels = states.Where(CInt(vowels >= 4)).ToList	 'Error	1	Operator '>=' is not defined for types 'System.Collections.Generic.IEnumerable(Of String)' and 'Integer'.   
ListBox2.DataSource = nrOfVowels

End Sub


#2 Dirk Andries

Dirk Andries

    Guru Developer

  • Leden
  • PipPipPipPipPipPip
  • 1164 berichten
    Laatst bezocht
  • LocatieGent

Geplaatst op 30 juni 2019 - 22:30

Bericht bekijkenludo64, op 30 juni 2019 - 10:55, zei:

Van 50 Amerikaanse staten (listbox1), moet ik degene die 4 of meer klinkers bevatten, in listbox2 zetten.
Visual Basic Code:
	  Dim states As New List(Of String)(
			{
			"Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut",
			"Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas",
			"Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi",
			"Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York",
			"North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island",
			"South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington",
			"West Virginia", "Wisconsin", "Wyoming"
			})

		ListBoxStates.DataSource = states
		Const vowels As String = "aeiouAEIOU"

		ListBoxSelectie.DataSource =
			states.Where(Function(s) s.Count(Function(k) vowels.Contains(k)) >= 4).ToList



#3 ludo64

ludo64

    Rookie Developer

  • Leden
  • Pip
  • 30 berichten
    Laatst bezocht 25 jul 2019 15:42
  • LocatieTongeren

Geplaatst op 30 juni 2019 - 23:03

Thx Dirk, je bent een topper

#4 Dirk Andries

Dirk Andries

    Guru Developer

  • Leden
  • PipPipPipPipPipPip
  • 1164 berichten
    Laatst bezocht
  • LocatieGent

Geplaatst op 01 juli 2019 - 11:33

Zonder Linq:
Visual Basic Code:

Private Sub FunMetKlinkers
Dim states As New List(Of String)(
		 {
		 "Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut",
		 "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas",
		 "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi",
		 "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York",
		 "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island",
		 "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington",
		 "West Virginia", "Wisconsin", "Wyoming"
		 })

	 ListBoxStates.DataSource = states
	 ListBoxSelectie.DataSource = SelecteerOpAantalKlinkers(states)
End Function

Function SelecteerOpAantalKlinkers(lijst As List(Of String)) As List(Of String)
	 Dim result As New List(Of String)
	 For Each item In lijst
		 If MinstensVierKlinkers(item) Then result.Add(item)
	 Next
	 Return result
End Function

Function MinstensVierKlinkers(bron As String) As Boolean
	 Return AantalKlinkers(bron) >= 4
End Function

Function AantalKlinkers(bron As String) As Integer
	 Dim result As Integer = 0
	 For Each karakter In bron
		 If IsKlinker(karakter) Then result += 1
	 Next
	 Return result
End Function

Function IsKlinker(karakter As Char) As Boolean
	 Return "aeiouAEIOU".Contains(karakter)
End Function








Ook met taq VS 2012, Visual Basic 2012 voorzien

0 gebruiker(s) lezen dit onderwerp

0 lid(leden), 0 bezoeker(s), 0 anonieme gebruikers

Inloggen


[VS 2012] Untitled 1

Met dank aan J├╝rgen voor de jarenlange inzet van visualbasic.be (anno dec 2000)
Met dank aan Mike en Ronneke voor de jarenlange inzet van vbib.be (anno dec 2010)
Met dank aan PascalBianca voor de jarenlange inzet van vbib.be (anno dec 2016)