Create own data examples in LinqPad - linqpad

I have a class (vb.net) with some data that I want to query in LinqPad. I already worked with some examples as the one from "Linq in Action" so they work with some kind of classes with data as well to explain queries. But I just cannot find anything about how to import or write your own classes. Could anyone help me here?
My Class looks like:
Public Class Employee
Public Property ID As Integer
Public Property Salery As Integer
Public Property Name As String
Public Property Department As String
Public Property Gender As String
Public Shared Function GetAllEmployees() As List(Of Employee)
Return New List(Of Employee) From { _
New Employee With {.ID = 1, .Name = "Mark", .Department = "HR", .Gender = "Male", .Salery = 12000},
New Employee With {.ID = 2, .Name = "Sandra", .Department = "IT", .Gender = "Female", .Salery = 2000} _
}
End Function
End Class

You might be missing a couple things about using LINQPad:
Set the Language to "VB Program" and put classes where the comment says to.
Use the Dump method to output an expression. (For "VB Expression", Dump is called automatically.)
Here is an example. (Note, you might be using that SQL-looking syntax.)
Sub Main
Employee.GetAllEmployees() _
.Where(Function (employee) employee.Department = "HR") _
.Dump()
Dim hrEmployees = From employee In Employee.GetAllEmployees()
Where employee.Department = "HR"
hrEmployees.Dump()
End Sub
' Define other methods and classes here
Public Class Employee
Public Property ID As Integer
Public Property Salery As Integer
Public Property Name As String
Public Property Department As String
Public Property Gender As String
Public Shared Function GetAllEmployees() As List(Of Employee)
Return New List(Of Employee) From { _
New Employee With {.ID = 1, .Name = "Mark", .Department = "HR", .Gender = "Male", .Salery = 12000},
New Employee With {.ID = 2, .Name = "Sandra", .Department = "IT", .Gender = "Female", .Salery = 2000} _
}
End Function
End Class

Related

using csvhelper writer with shouldquote in vb.net

I can see how shouldquote works with c# but does anyone have an example in vb.net?
I need to wrap every field with chr(34)
Private Sub Main()
Dim records = New List(Of Foo) From {
New Foo With {
.Id = 1,
.Name = "one"
}
}
Dim config = New CsvConfiguration(CultureInfo.InvariantCulture) With {
.ShouldQuote = Function(args) True
}
Using csv = New CsvWriter(Console.Out, config)
csv.WriteRecords(records)
End Using
End Sub
Public Class Foo
Public Property Id As Integer
Public Property Name As String
End Class

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)

object Collection Containing a List of IDs Linq

I have a list box and the user is able to multi-select. I want to use Linq and bring back the records of the selected IDs that the user selects. I need to bring back the full object record for each selected ID
Here is the contact object along with collection object
Namespace MODEL
<System.Serializable()> _
Public Class ContactCollection
Inherits System.Collections.ObjectModel.Collection(Of Contact)
Implements IList(Of Contact)
End Class
End Namespace
Namespace MODEL
<System.Serializable()> _
Public Class Contact
Private mContactID As Int32 = 0
Private mFirstName As String
Private mLastName As String
Public Property ContactID As Int32
Get
Return mContactID
End Get
Set(value As Int32)
mContactID = value
End Set
End Property
Public Property FirstName As String
Get
Return mFirstName
End Get
Set(value As String)
mFirstName = value
End Set
End Property
Public Property LastName As String
Get
Return mLastName
End Get
Set(value As String)
mLastName = value
End Set
End Property
End Class
End Namespace
Adding 5 Records to the collection object
Dim objCollection As New MODEL.ContactCollection
Dim obj As New MODEL.Contact
objCollection.Add(New MODEL.Contact With {
.ContactID = 1, _
.FirstName = "John", _
.LastName = "Smtih" _
})
objCollection.Add(New MODEL.Contact With {
.ContactID = 2, _
.FirstName = "Mark", _
.LastName = "Davis" _
})
objCollection.Add(New MODEL.Contact With {
.ContactID = 3, _
.FirstName = "Tom", _
.LastName = "Howe" _
})
objCollection.Add(New MODEL.Contact With {
.ContactID = 4, _
.FirstName = "Jerry", _
.LastName = "Thomas" _
})
objCollection.Add(New MODEL.Contact With {
.ContactID = 5, _
.FirstName = "Jane", _
.LastName = "Marry" _
})
This is the selected contact List from the list box
Dim lstContacts As New List(Of Integer)
lstContacts.Add(2)
lstContacts.Add(4)
I am not sure what to do at this point with Linq to find the values. I think I have to use contains but I have tried may different ways but I was unable to get the values.
I have tried this Linq but does not work or bring any records back
Dim objSearch from SearchContacts in objCollection
Where (lstContacts.Contains(SearchContacts.ContactID))
To get the Ids, try that :
Dim ids As IEnumerable(Of Int32) = myListBox.SelectedItems _
.OfType(Of Contact)() _
.Select( Function(c) c.ContactID ) _
Edit
If you want the Contacts, you can just just :
Dim ids As IEnumerable(Of Contact) = myListBox.SelectedItems _
.OfType(Of Contact)()
And if you want the contacts in a separate copied collection, you can :
Dim ids As List(Of Contact) = myListBox.SelectedItems _
.OfType(Of Contact)() _
.ToList()
Last (if think this is your real question - just tell and I erase everything above)
Dim selectedContacts As IEnumerable(Of MODEL.Contact) = From contact In objCollection
Join id In lstContacts
On contact.ContactID Equals id
Select contact

Left Outer Join doesnt show empty elements

I try to perform an left outer join within linq. My Datasource is:
Public Class Department
Public Property ID As Integer
Public Property Name As String
Public Shared Function GetAllDepartments() As List(Of Department)
Return New List(Of Department) From { _
New Department With {.ID = 1, .Name = "IT"},
New Department With {.ID = 2, .Name = "HR"},
New Department With {.ID = 3, .Name = "Payroll"}
}
End Function
End Class
Public Class Employee
Public Property ID As Integer
Public Property Name As String
Public Property DepartmentID As Integer
Public Shared Function GetAllEmployees() As List(Of Employee)
Return New List(Of Employee) From { _
New Employee With {.ID = 1, .Name = "Mark", .DepartmentID = 1},
New Employee With {.ID = 10, .Name = "Andy"}
}
End Function
End Class
My query is:
'Left Outer Join
Dim result = From emp In Employee.GetAllEmployees
Group Join dep In Department.GetAllDepartments
On emp.DepartmentID Equals dep.ID Into eGroup = Group
From all In eGroup.DefaultIfEmpty
Select New With {
.Person = emp.Name,
.DepartmentName = If(all.Name Is Nothing, "No Department", all.Name)}
What I expect to get is:
Mark IT
Andy No Department
But what I get is
Mark IT
Could anyone tell me, what is wrong with the query? I just cannot see it, even after reading msdn help for it over again, I just cannot find what is wrong.
I cannot reproduce this behavior because when I try to run your code it throws a NullReferenceException.
However, if I change this code:
.DepartmentName = If(all.Name Is Nothing, "No Department", all.Name)}
To this code:
.DepartmentName = If((all Is Nothing), "No Department", all.Name)}
I get the expected output:
Mark, ITAndy, No Department

LINQ to Objects, query arrays in object

I need to query an array. I have tried to use LINQ but with no success.
Here is a simplified structure of Insurance
Public Class Insurance
Public _typ As String
Public _numb As Number()
Public _indi As Indicator()
End Class
Public Class Number
Sub New(ByVal n As String, ByVal i As String)
Me._ntype = n
Me._value = i
End Sub
Public _ntype As String
Public _value As String
End Class
Public Class Indicator
Sub New(ByVal it As String, ByVal ind As String)
Me._itype = it
Me._ind = ind
End Sub
Public _itype As String
Public _ind As String
End Class
Here I have construct a little example to explain how the array of Insurance looks. In real there is a Service who delivers the array to me.
Dim insarray(4) As Insurance
Dim insa As New Insurance
insa._typ = "SJUKVARD"
insa._indi = New Indicator() {New Indicator("ST", "G"), New Indicator("TAX", "P"), New Indicator("PT", "6")}
insa._numb = New Number() {New Number("PIN", "1000"), New Number("AGN", "A0001")}
insarray(0) = insa
Dim insa2 As New Insurance
insa2._typ = "SJUKVARD"
insa2._indi = New Indicator() {New Indicator("ST", "G"), New Indicator("TAX", "P")}
insa2._numb = New Number() {New Number("PIN", "2000"), New Number("AGN", "A0002")}
insarray(1) = insa2
Dim insa3 As New Insurance
insa3._typ = "SJUKVARD"
insa3._indi = New Indicator() {New Indicator("ST", "G"), New Indicator("KAL", "T")}
insa3._numb = New Number() {New Number("PIN", "3000"), New Number("AGN", "A0003")}
insarray(2) = insa3
Dim insa4 As New Insurance
insa4._typ = "SJUKVARD"
insa4._indi = New Indicator() {New Indicator("ST", "G"), New Indicator("TAX", "P")}
insa4._numb = New Number() {New Number("PIN", "4000")}
insarray(3) = insa4
Dim insa5 As New Insurance
insa5._typ = "SJUK"
insa5._indi = New Indicator() {New Indicator("ST", "F"), New Indicator("TAX", "P")}
insa5._numb = New Number() {New Number("PIN", "5000"), New Number("AGN", "A0005")}
insarray(4) = insa5
Dim myIns As IEnumerable(Of Object)
myIns = ...(here should to LINQ question be)
In "myIns= ..." I want to construct a LINQ query which
look for Insurances in the array which have _typ="SJUKVARD"
and in the Insurance _numb array if some of the Number objects has
_ntype="AGN"
and in the Insurance _indi array if some of the Indicator objects has
_itype="TAX"
and in the Insurance _indi array if some of the Indicator objects has
_itype="ST" AND _ind="G"
and in the Insurance _indi array NOT has a Indicator objects with
_type="PT
So the only hit will be Insurance "insa2". Is this possible with LINQ?
Hope someone can help me :)
myIns = insarray.Where(Function(i) i._typ = "SJUKVARD" AndAlso _
i._numb.Any(Function (n) n._ntype = "AGN") AndAlso _
i._indi.All(function(ind) (ind._itype <> "PT") AndAlso _
(ind._itype = "TAX" OrElse ( ind._itype = "ST" AndAlso ind._ind="G"))))