Dictionary (of integer,class) type - vb.net

I would like to save variables in a simple method before adding to a datatable. Is it possible to have a dictionary (of integer,class) type like the code below? I can then loop through the keys to get the variables.Any ideas please?
Public Class CONTACTS
Dim FIRST_NAME As String
Dim SURNAME As String
Dim DOB As Date
Dim AGE As Integer
End Class
Sub TEST_DICT()
Dim dctCONTACTS As Dictionary(Of Integer, List OF Class(CONTACTS)) 'not sure how to declare this
dctCONTACTS.Add(1, "MARK").FIRST_NAME 'not sure how to savevariable
dctCONTACTS.Add(1, "SMITH").SURNAME
dctCONTACTS.Add(1, "5 May 1995").DOB
dctCONTACTS.Add(1, 31).AGE
dctCONTACTS.Add(2, "ANNE").FIRST_NAME
dctCONTACTS.Add(2, "MOORE").SURNAME
dctCONTACTS.Add(3, "5 April 1990").DOB
dctCONTACTS.Add(4, 26).AGE
Dim X = dctCONTACTS(1).AGE
End Sub

You really you go look at the many many "how to code VB.NET" resources online rather than ask basic syntax questions here.
Here is your code in valid VB.NET:
Public Class CONTACTS
Public FIRST_NAME As String
Public SURNAME As String
Public DOB As Date
Public AGE As Integer
End Class
Sub TEST_DICT()
Dim dctCONTACTS As New Dictionary(Of Integer, List(Of CONTACTS))
dctCONTACTS.Add(1, New List(Of CONTACTS) From
{
New CONTACTS() With { .FIRST_NAME = "MARK", .SURNAME = "SMITH", .DOB = New DateTime(1995, 5, 5), .AGE = 31 },
New CONTACTS() With { .FIRST_NAME = "JOE", .SURNAME = "SMITH", .DOB = New DateTime(1995, 5, 5), .AGE = 31 },
New CONTACTS() With { .FIRST_NAME = "SARAH", .SURNAME = "SMITH", .DOB = New DateTime(1995, 5, 5), .AGE = 31 }
})
dctCONTACTS.Add(2, New List(Of CONTACTS) From
{
New CONTACTS() With { .FIRST_NAME = "BOB", .SURNAME = "SMITH", .DOB = New DateTime(1995, 5, 5), .AGE = 31 },
New CONTACTS() With { .FIRST_NAME = "SUE", .SURNAME = "SMITH", .DOB = New DateTime(1995, 5, 5), .AGE = 31 },
New CONTACTS() With { .FIRST_NAME = "ABIGAIL", .SURNAME = "SMITH", .DOB = New DateTime(1995, 5, 5), .AGE = 31 }
})
Console.WriteLine(dctCONTACTS(2)(0).FIRST_NAME)
End Sub
Running TEST_DICT gives BOB in this case.

Related

vb,net my collection property in my class is throwing an error

I am learning about collections, I have a Person class
Imports System
Imports System.Collections.Generic
Imports System.Text
Public Class Person
Public Sub New()
End Sub
Public Sub New(ByVal id As Integer, ByVal first_name As String, ByVal mid_name As String, ByVal last_name As String, ByVal age As Short, ByVal sex As Char)
Me.p_id = id
Me.first_name = first_name
Me.mid_name = mid_name
Me.last_name = last_name
Me.p_age = age
Me.p_sex = sex
End Sub
Private p_id As Integer = -1
Private first_name As String = String.Empty
Private mid_name As String = String.Empty
Private last_name As String = String.Empty
Private p_age As Short = 0
Private p_sex As Nullable(Of Char) = Nothing
Public Property ID() As Integer
Get
Return p_id
End Get
Set(ByVal value As Integer)
p_id = value
End Set
End Property
Public Property FirstName() As String
Get
Return first_name
End Get
Set(ByVal value As String)
first_name = value
End Set
End Property
Public Property MiddleName() As String
Get
Return mid_name
End Get
Set(ByVal value As String)
mid_name = value
End Set
End Property
Public Property LastName() As String
Get
Return last_name
End Get
Set(ByVal value As String)
last_name = value
End Set
End Property
Public Property Age() As Short
Get
Return p_age
End Get
Set(ByVal value As Short)
p_age = value
End Set
End Property
Public Property Sex() As Nullable(Of Char)
Get
Return p_sex
End Get
Set(ByVal value As Nullable(Of Char))
p_sex = value
End Set
End Property
End Class
and an Employee Class where I define a Person property
Imports System
Imports System.Collections.Generic
Imports System.Text
Public Class Employee
Public Sub New()
End Sub
Public Sub New(ByVal id As Integer, ByVal companyName As String, ByVal office As String, colPerson As Person)
'Me._employeeid = id
'Me._companyName = companyName
'Me._office = office
End Sub
Private _employeeid As Integer = -1
Private _companyName As String = String.Empty
Private _office As String = String.Empty
Public Property Empoyee_ID() As Integer
Get
Return _employeeid
End Get
Set(ByVal value As Integer)
_employeeid = value
End Set
End Property
Public Property CompanyName() As String
Get
Return _companyName
End Get
Set(ByVal value As String)
_companyName = value
End Set
End Property
Public Property Office() As String
Get
Return _office
End Get
Set(ByVal value As String)
_office = value
End Set
End Property
Property colPerson As List(Of Person)
End Class
How can i populate the persons class as well
Sub Main()
Dim pList As List(Of Person) = New List(Of Person)()
Dim thePerson As New List(Of Person) From
{
New Person With {.Age = 29, .FirstName = "John", .LastName = "Shields", .MiddleName = "", .Sex = "M", .ID = 1},
New Person With {.Age = 34, .FirstName = "Mary", .LastName = "Matthew", .MiddleName = "L", .Sex = "F", .ID = 2},
New Person With {.Age = 55, .FirstName = "Amber", .LastName = "Carl", .MiddleName = "P", .Sex = "M", .ID = 3},
New Person With {.Age = 12, .FirstName = "Kathy", .LastName = "Berry", .MiddleName = "O", .Sex = "F", .ID = 4}
}
'pList.Add(New Person(1, "John", "", "Shields", 29, "M"c))
'pList.Add(New Person(2, "Mary", "Matthew", "Jacobs", 35, "F"c))
'pList.Add(New Person(3, "Amber", "Carl", "Agar", 25, "M"c))
'pList.Add(New Person(4, "Kathy", "", "Berry", 21, "F"c))
'pList.Add(New Person(5, "Lena", "Ashco", "Bilton", 33, "F"c))
'pList.Add(New Person(6, "Susanne", "", "Buck", 45, "F"c))
'pList.Add(New Person(7, "Jim", "", "Brown", 38, "M"c))
'pList.Add(New Person(8, "Jane", "G", "Hooks", 32, "F"c))
'pList.Add(New Person(9, "Robert", "", "", 31, "M"c))
'pList.Add(New Person(10, "Cindy", "Preston", "Fox", 25, "F"c))
'pList.Add(New Person(11, "Gina", "", "Austin", 27, "F"c))
'pList.Add(New Person(12, "Joel", "David", "Benson", 33, "M"c))
'pList.Add(New Person(13, "George", "R", "Douglas", 55, "M"c))
'pList.Add(New Person(14, "Richard", "", "Banks", 22, "M"c))
'pList.Add(New Person(15, "Mary", "C", "Shaw", 39, "F"c))
'
'loop through the list
' PrintOnConsole(pList, "1. --- Looping through all items in the List<T> ---")
'
'Filtering List(T) using a single condition - (Age > 35)
'Dim filterOne As List(Of Person) = pList.FindAll(Function(p As Person) p.Age > 35)
'PrintOnConsole(filterOne, "2. --- Filtering List<T> on single condition (Age > 35) ---")
''
'' Filtering List(T) on multiple conditions (Age > 35 and Sex is Female)
'Dim filterMultiple As List(Of Person) = pList.FindAll(Function(p As Person) p.Age > 35 AndAlso p.Sex = "F"c)
'PrintOnConsole(filterMultiple, "3. --- Filtering List<T> on multiple conditions (Age > 35 and Sex is Female) ---")
''
''Sorting List(T) (Sort on FirstName)
'Dim sortFName As List(Of Person) = pList
'sortFName.Sort(Function(p1 As Person, p2 As Person) p1.FirstName.CompareTo(p2.FirstName))
'PrintOnConsole(sortFName, "4. --- Sort List<T> (Sort on FirstName) ---")
'
'Sorting List(T) descending (Sort on LastName descending)
'Dim sortLNameDesc As List(Of Person) = pList
'sortLNameDesc.Sort(Function(p1 As Person, p2 As Person) p2.LastName.CompareTo(p1.LastName))
'PrintOnConsole(sortLNameDesc, "5. --- Sort List<T> descending (Sort on LastName descending) ---")
''Add new List(T) to existing List(T)
'Dim newList As List(Of Person) = New List(Of Person)()
'newList.Add(New Person(16, "Geoff", "", "Fisher", 29, "M"c))
'newList.Add(New Person(17, "Samantha", "Carl", "Baxer", 32, "F"c))
'pList.AddRange(newList)
'PrintOnConsole(pList, "6. --- Add new List<T> to existing List<> ---")
''Remove multiple items from List(T) based on condition (remove male employees)
'Dim removeList As List(Of Person) = pList
'removeList.RemoveAll(Function(p As Person) p.Sex = "M"c)
'PrintOnConsole(removeList, "7. --- Remove multiple items from List<> based on condition ---")
'' Create Read Only List(T)
'Console.WriteLine("Create Read Only List<>")
'Dim personReadOnly As IList(Of Person) = pList
'Console.WriteLine("Before - Is List Read Only? True or False : " & personReadOnly.IsReadOnly)
'personReadOnly = pList.AsReadOnly()
'Console.WriteLine("After - Is List Read Only? True or False : " & personReadOnly.IsReadOnly & "</br>")
'
'Dim pList1 As New Person
Dim emp As New List(Of Employee)
Dim r As New Employee
r.CompanyName = "zac"
r.Office = "home"
r.Empoyee_ID = 1
'Dim pList1 = New Person(1, "John", "", "Shields", 29, "M"c)
r.colPerson.Add(New Person(1, "John", "", "Shields", 29, "M"c))---> Gives error
emp.Add(r)
'
Dim i As New Employee
i.CompanyName = "zac1"
i.Office = "home1"
i.Empoyee_ID = 2
i.colPerson.Add(New Person(3, "Amber", "Carl", "Agar", 25, "M"c))
pList.Add(New Person(4, "Kathy", "", "Berry", 21, "F"c))
emp.Add(i)
'
Dim t As New Employee
t.CompanyName = "zac2"
t.Office = "home2"
t.Empoyee_ID = 2
pList.Add(New Person(5, "Lena", "Ashco", "Bilton", 33, "F"c))
pList.Add(New Person(6, "Susanne", "", "Buck", 45, "F"c))
emp.Add(t)
For Each item In emp
'item.CompanyName = "zac"
'item.Office = "home"
'item.colperson.Where(Function(x) x.ID = 17)
'Console.WriteLine("employee with person collection: " & item.CompanyName & " " & item.Office & " " & item.colperson.Where(Function(x) x.ID = 17).ToString & "</br>")
Console.WriteLine("employee with person collection: " & item.CompanyName & " " & item.Office & "</br>")
Next
End Sub
r.colPerson.Add(New Person(1, "John", "", "Shields", 29, "M"c))---> Gives error
It gives an error because even though colPerson is declared to be capable of holing a list of people, it hasn't actually been set to be a list of people, so it's currently Nothing, and you can't call methods on something that is Nothing
Property colPerson As New List(Of Person)
^^^
Add a New directive to ensure it's declared and initialized to an instance of a List
Also, please:
Don't put "col" in names
Use a plural name for List(Of Thing) - this is a list of person so it should at least be called People, but also perhaps state what kind of people they are. For example if this Employee was recommended by a few people, call it RcommendedByPeople
Only use Collection in a name if you're writing a class that is a collection, such as Microsoft did when they wrote MatchCollection - a collection of regular expression Matches
Be definite about whether the property is public, private etc
i.e.
Public Property RcommendedByPeople As New List(Of Person)

Linq to SQL in VS 2019 using VB.NET - Outer left join?

I'm struggling to get the result I need from my linq to SQL query. I can find all of the person table records, or only the ones that have a corresponding date_table entry. This is the query I'm using, this results in only MK numbers 101 and 104 being returned. Numbers 100 and 105 are missed I guess because they don't have a valid record for today?
Where Person_data_table.MK = Date_Table.FK And Date_Table.Allocated <> "Vacation" And Date_Table.Allocated <> "Sick" And Date_Table.Date = Today And Person_data_table.Team = "A"
This is the table data (note Today is 15-Mar-20) - Please keep answers in VB, as C# answers don't tend to work after I have translated them! - Any help appreciated
regards
Peter
Notice that you also posted your question here.
Is your question still unresolved? You can try my code as follows. Sorry I didn't use SQL, I used class, but that didn't affect the result.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim MK100 As NewPerson = New NewPerson With {
.MK = "100",
.First_Name = "Mike",
.Last_Name = "Smith",
.Team = "A"
}
Dim MK101 As NewPerson = New NewPerson With {
.MK = "101",
.First_Name = "Dave",
.Last_Name = "Sharp",
.Team = "A"
}
Dim MK102 As NewPerson = New NewPerson With {
.MK = "102",
.First_Name = "Roger",
.Last_Name = "Hands",
.Team = "A"
}
Dim MK103 As NewPerson = New NewPerson With {
.MK = "103",
.First_Name = "Keely",
.Last_Name = "Riller",
.Team = "A"
}
Dim MK104 As NewPerson = New NewPerson With {
.MK = "104",
.First_Name = "Simon",
.Last_Name = "Says",
.Team = "A"
}
Dim MK105 As NewPerson = New NewPerson With {
.MK = "105",
.First_Name = "Eugene",
.Last_Name = "Phillips",
.Team = "A"
}
Dim MK106 As NewPerson = New NewPerson With {
.MK = "106",
.First_Name = "William",
.Last_Name = "Nobody",
.Team = "B"
}
Dim FK101 As NewDate = New NewDate With {
.FK = "101",
.Date_ = "15-Mar-20",
.Allocated = "Job Number 1"
}
Dim FK102 As NewDate = New NewDate With {
.FK = "102",
.Date_ = "15-Mar-20",
.Allocated = "Vacation"
}
Dim FK103 As NewDate = New NewDate With {
.FK = "103",
.Date_ = "15-Mar-20",
.Allocated = "Sick"
}
Dim FK104 As NewDate = New NewDate With {
.FK = "104",
.Date_ = "15-Mar-20",
.Allocated = "Job nunber 2"
}
Dim Person_Data_table As List(Of NewPerson) = New List(Of NewPerson) From {
MK100,
MK101,
MK102,
MK103,
MK104,
MK105,
MK106
}
Dim Date_Table As List(Of NewDate) = New List(Of NewDate) From {
FK101,
FK102,
FK103,
FK104
}
Dim query = From NewPerson In Person_Data_table Group Join NewDate In Date_Table On NewPerson.MK Equals NewDate.FK Into gj = Group From subperson In gj.DefaultIfEmpty() Where (NewPerson.Team = "A") AndAlso (subperson Is Nothing OrElse ((subperson.Allocated <> "Vacation") AndAlso (subperson.Allocated <> "Sick"))) Select New With {NewPerson.MK, NewPerson.First_Name, NewPerson.Last_Name, NewPerson.Team
}
For Each v In query
ListBox1.Items.Add($"{v.MK,-15}{v.First_Name,-15}{v.Last_Name,-15}{v.Team}")
Next
End Sub
End Class
Class NewPerson
Public Property MK As String
Public Property First_Name As String
Public Property Last_Name As String
Public Property Team As String
End Class
Class NewDate
Public Property FK As String
Public Property Date_ As String
Public Property Allocated As String
End Class
And the effect is as follows:

Visual Studio 2019 Error BC30311 Value of type 'VehicleMake' cannot be converted to 'VehicleContext'

I have project with VB and Entity Framework. I am stuck here. Can someone help me? I don't know how to fix this. I am following tutorial but tutorial use .cshtml and I use .vbhtml.
I am stuck here for 2 hours and can't find solution. Microsoft online help is useless, can't find anything that is good and helps me.
Thanks for your help.
Mono is name of my project
VehicleModel.vb
Imports System.ComponentModel.DataAnnotations.Schema
Namespace Vehicle.Models
Public Class VehicleModel
<DatabaseGenerated(DatabaseGeneratedOption.None)>
Public Property Id As Integer
Public Property MakeId As Integer
Public Property Name As String
Public Property Abrv As String
Public Overridable Property VehicleMake As ICollection(Of VehicleMake)
End Class
End Namespace
VehicleInitilizer.vb
Imports Mono.Vehicle.Models
Namespace Vehicle.DAL
Public Class VehicleInitializer
Inherits Entity.DropCreateDatabaseIfModelChanges(Of VehicleContext)
Protected Overrides Sub Seed(ByVal context As VehicleContext)
Dim VehicleMake = New List(Of VehicleMake) From {
New VehicleMake With {
.Id = 1,
.Name = "BMW",
.Abrv = "BMW"
},
New VehicleMake With {
.Id = 2,
.Name = "Ford",
.Abrv = "Ford"
},
New VehicleMake With {
.Id = 3,
.Name = "Volkswagen",
.Abrv = "VW"
},
New VehicleMake With {
.Id = 4,
.Name = "Hyundai",
.Abrv = "Hyundai"
},
New VehicleMake With {
.Id = 5,
.Name = "Mercedes-Benz",
.Abrv = "Mercedes"
}
}
vehicleMake.ForEach(Sub(s) context.VehicleMake.Add(s))
context.SaveChanges()
Dim VehicleModel = New List(Of VehicleModel) From {
New VehicleModel With {
.Id = 1,
.MakeId = 1,
.Name = "128",
.Abrv = "BMW"
},
New VehicleModel With {
.Id = 2,
.MakeId = 1,
.Name = "325",
.Abrv = "BMW"
},
New VehicleModel With {
.Id = 3,
.MakeId = 1,
.Name = "X5",
.Abrv = "BMW"
},
New VehicleModel With {
.Id = 1,
.MakeId = 2,
.Name = "Mondeo",
.Abrv = "Ford"
},
New VehicleModel With {
.Id = 2,
.MakeId = 2,
.Name = "Focus",
.Abrv = "Ford"
},
New VehicleModel With {
.Id = 3,
.MakeId = 2,
.Name = "Fiesta",
.Abrv = "Ford"
},
New VehicleModel With {
.Id = 1,
.MakeId = 3,
.Name = "Golf",
.Abrv = "VW"
},
New VehicleModel With {
.Id = 2,
.MakeId = 3,
.Name = "Passat",
.Abrv = "VW"
},
New VehicleModel With {
.Id = 3,
.MakeId = 3,
.Name = "Arteon",
.Abrv = "VW"
},
New VehicleModel With {
.Id = 1,
.MakeId = 4,
.Name = "i30",
.Abrv = "Hyundai"
},
New VehicleModel With {
.Id = 2,
.MakeId = 4,
.Name = "Tuscon",
.Abrv = "Hyundai"
},
New VehicleModel With {
.Id = 3,
.MakeId = 4,
.Name = "Kona",
.Abrv = "Hyundai"
},
New VehicleModel With {
.Id = 1,
.MakeId = 5,
.Name = "C-Class",
.Abrv = "Mercedes"
},
New VehicleModel With {
.Id = 2,
.MakeId = 5,
.Name = "E-Class",
.Abrv = "Mercedes"
},
New VehicleModel With {
.Id = 3,
.MakeId = 5,
.Name = "A-Class",
.Abrv = "Mercedes"
}
}
VehicleMake.ForEach(Sub(s) context.VehicleModel.Add(s))
context.SaveChanges()
End Sub
End Class
End Namespace
VehicleContext.vb
Imports System.Data.Entity
Imports System.Data.Entity.ModelConfiguration.Conventions
Namespace Vehicle.DAL
Public Class VehicleContext
Inherits DbContext
Public Sub New()
MyBase.New("VehicleContext")
End Sub
Public Property VehicleMake As DbSet(Of VehicleContext)
Public Property VehicleModel As DbSet(Of VehicleContext)
End Class
End Namespace
VehicleMake.vb
Imports System
Imports System.Collections.Generic
Namespace Vehicle.Models
Public Class VehicleMake
Public Property Id As Integer
Public Property Name As String
Public Property Abrv As String
Public Overridable Property VehicleModel As ICollection(Of VehicleModel)
End Class
End Namespace
At first glance you seem to have a number of problems in your code.
Here:
Dim VehicleMake = New List(Of VehicleMake) From { ... you are creating a list with the same name as your class VehicleMake. that's very confusing, i suggest naming it something else (VehiclesMakes for instance).
But anyway, later on when you access the list here : vehicleMake.ForEach(Sub(s)context.VehicleMake.Add(s)) You are accessing the a Property named VehicleMake in your class VehicleContext. Again, you are accessing a property in a class, and that property is named identically to a class you have created. but your property VehicleMake is not actually an object of the Class VehicleMake, this is all very confusing.
The Add Method of your Property VehicleMake expects an argument of type VehicleContext (because it is defined as Public Property VehicleMake As DbSet(Of VehicleContext)). But instead, you give it an instance of you Class VehicleMake, from your List that's also named VehicleMake.
You get the error because VehicleMake is given where VehicleContext is expected. and VehicleMake does not inherited from VehicleContext. Thus, it cannot be converted to it.

Multi property lambda groupby does not return correct result

Trying to group a list of records with lambda but does not work with multiple property
I hate stake over flow, here is a bunch of text that is not need because stack over flow say I need more text because there is too much code and there stupid validation system keeps say my code is incorrectly formatted and I have spent to much time trying to get this to post
Module Module1
Sub Main()
Dim list As New List(Of MyObject)
list.Add(New MyObject() With {.Batch = "", .Expiry = New Date(2001, 1, 1), .Serial = "S1"})
list.Add(New MyObject() With {.Batch = "", .Expiry = New Date(2001, 1, 1), .Serial = "S5"})
list.Add(New MyObject() With {.Batch = "", .Expiry = New Date(2001, 1, 2), .Serial = "S2"})
list.Add(New MyObject() With {.Batch = "", .Expiry = New Date(2001, 1, 3), .Serial = "S3"})
list.Add(New MyObject() With {.Batch = "", .Expiry = New Date(2001, 1, 4), .Serial = "S4"})
Dim distinct As List(Of MyObject) = list.GroupBy(Function(x) New With {Key .Batch = x.Batch.ToUpper().Trim(), .Expiry = x.Expiry.Date}, Function(x) x).Select(Function(x) New MyObject() With {.Batch = x.Key.Batch, .Expiry = x.Key.Expiry, .Serial = ""}).ToList()
End Sub
Public Class MyObject
Property Batch As String
Property Expiry As Date
Property Serial As String
End Class
End Module
'Expected Results:
'item 0: Batch = "", Expiry = 2001/1/01, Serial = ""
'item 1: Batch = "", Expiry = 2001/1/02, Serial = ""
'item 2: Batch = "", Expiry = 2001/1/03, Serial = ""
'item 3: Batch = "", Expiry = 2001/1/04, Serial = ""
'Actual Results:
'item 0: Batch = "", Expiry = 2001/1/01, Serial = ""
You have miss a keyword Key in your group by
Your code:
list.GroupBy(Function(x) New With {Key .Batch = x.Batch.ToUpper().Trim(), .Expiry = x.Expiry.Date}, Function(x) x)
But it should be
list.GroupBy(Function(x) New With {Key .Batch = x.Batch.ToUpper().Trim(), Key .Expiry = x.Expiry.Date}, Function(x) x)
Fiddler

How to get Lambda in LINQ to actually filter for dynamic linq

Example-I have a person class
Public Class Person
Private _fname As String
Public Property Fname() As String
Get
Return _fname
End Get
Set(ByVal value As String)
_fname = value
End Set
End Property
Private _lname As String
Public Property Lname() As String
Get
Return _lname
End Get
Set(ByVal value As String)
_lname = value
End Set
End Property
Private _age As Integer
Public Property Age() As Integer
Get
Return _age
End Get
Set(ByVal value As Integer)
_age = value
End Set
End Property
End Class
Dim people As New List(Of Person)
people.Add(New Person With {.Fname = "Alice", .Lname = "Apples", .Age = 1})
people.Add(New Person With {.Fname = "Bob", .Lname = "Banana", .Age = 2})
people.Add(New Person With {.Fname = "Charlie", .Lname = "Cherry", .Age = 3})
people.Add(New Person With {.Fname = "Dave", .Lname = "Durian", .Age = 4})
people.Add(New Person With {.Fname = "Eric", .Lname = "EggPlant", .Age = 10})
Dim filteredPerson = From person In people
filteredPerson.Where(Function(fp) fp.Fname = "Bob")
Dim finalList = filteredPerson.ToList
For Each p In finalList
Debug.Print("FNAME: " + p.Fname)
Next
This still returns all 5 people, like the where is not being applied, what am I doing wrong?
I would also like to be able to pass a list of names and return only those
Dim searchList As New List(Of String)
searchList.Add("Bob")
searchList.Add("Dave")
Dim filteredPerson = From person In people
For Each s In searchList
Dim innerName As String = s
filteredPerson.Where(Function(fp) fp.Fname = innerName)
Next
Dim finalList = filteredPerson.ToList
For Each p In finalList
Debug.Print("FNAME: " + p.Fname)
Next
The problem is that Where doesn't change the collection. It returns the newly filtered collection.
Try this:
Dim filteredPerson = people.Where(Function(fp) fp.Fname = "Bob")
(By the way, I don't see anything dynamic in here... where are you using dynamic LINQ?)
To add multiple Where clauses, you'll want something like this:
Dim searchList As New List(Of String)
searchList.Add("Bob")
searchList.Add("Dave")
Dim filteredPerson As IEnumerable(Of String) = people
For Each s In searchList
Dim innerName As String = s
filteredPerson = filteredPerson.Where(Function(fp) fp.Fname = innerName)
Next
Dim finalList = filteredPerson.ToList
For Each p In finalList
Debug.Print("FNAME: " + p.Fname)
Next
However, I don't believe that's actually what you want to do. Each Where clause is going to insist that Fname is the specified name - and it's not going to be both Bob and Dave! I think you actually want something which can be expressed much more simply:
Dim searchList As New List(Of String)
searchList.Add("Bob")
searchList.Add("Dave")
Dim filteredPerson = people.Where(Function(fp) searchList.Contains(fp.Fname))
Dim finalList = filteredPerson.ToList
For Each p In finalList
Debug.Print("FNAME: " + p.Fname)
Next
All we want to know is whether Fname is in searchList, which is what Contains does.
Dim people As New List(Of Person)
people.Add(New Person With {.Fname = "Alice", .Lname = "Apples", .Age = 1})
people.Add(New Person With {.Fname = "Bob", .Lname = "Banana", .Age = 2})
people.Add(New Person With {.Fname = "Charlie", .Lname = "Cherry", .Age = 3})
people.Add(New Person With {.Fname = "Dave", .Lname = "Durian", .Age = 4})
people.Add(New Person With {.Fname = "Eric", .Lname = "EggPlant", .Age = 10})
Dim searchList As New List(Of String)
searchList.Add("Bob")
searchList.Add("Dave")
dim filteredItems = from p in people _
join i in searchList on p.FName equals i _
select p
dim personFound as Person
for each personFound in filteredItems
Console.WriteLine(personFound.Lname)
next