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.
Quiero llamar algo como:
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
'-- 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"
preguntado el 27 de agosto de 11 a las 15:08
In your first LINQ query, you are defining an anonymous type. If you want you can put almost the whole code block (both
LT) in a single method and return LT. Since
LT contiene un
IEnumerable(Of T) con
T being an anonymous type, you can let your method simply return an
IEnumerable, como sigue:
Public Shared Function GetAllPeople() As IEnumerable ' code here End Function
This will work in ASP.NET Web Forms, since Web Forms uses reflection to load the data from members that do define (
PeopleID). However, this approach is a bit fragile, since there is no compile time support. Another option therefore is to define a special
PersonDTO) that contains two public properties
PeopleID and let that method return an
IEnumerable(Of PersonDTO) or even better an array of