The code below is used in a Linq to SQL solution, and is in the Load event of a form.
It works fine to display an ordered list of people’s names, and set the value member to that person’s ID.
However, I’m going to be using these ‘people’ comboboxes a lot, so I’d like to put a sub into my Utility code there is only one line in the form’s Load event.
I want to call something like: Call ComboboBoxPeople(cbo, tblTurnbackMain, ReportedByID)
The sub would begin like: Public Sub ComboboxPeople(cbo as Combobox, tbl as 'sometype', fld as 'someothertype')
What can I use as the parameter types for tbl and fld?
'-- cboReportedBy datasource
Dim LQ = (From p In TurnbackDC.vewPeopleAll, t In TurnbackDC.tblTurnbackMain
Where p.PeopleID = t.ReportedByID
Select p.Person, p.PeopleID).Distinct()
Dim LT = From x In LQ
Order By x.Person
Select x.Person, x.PeopleID
cboReportedBy.DataSource = LT
cboReportedBy.DisplayMember = "Person"
cboReportedBy.ValueMember = "PeopleID"
Thanks!
Dan
In your first LINQ query, you are defining an anonymous type. If you want you can put almost the whole code block (both
LQandLT) in a single method and return LT. SinceLTcontains anIEnumerable(Of T)withTbeing an anonymous type, you can let your method simply return anIEnumerable, as follows:This will work in ASP.NET Web Forms, since Web Forms uses reflection to load the data from members that do define (
PersonandPeopleID). However, this approach is a bit fragile, since there is no compile time support. Another option therefore is to define a specialClass(let’s call itPersonDTO) that contains two public propertiesPersonandPeopleIDand let that method return anIEnumerable(Of PersonDTO)or even better an array ofPersonDTOobjects.