Cómo pasar tipos Linq a SQL a una función

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 tbl y 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"

¡Gracias! Dan

preguntado el 27 de agosto de 11 a las 15:08

¿Cuáles son los tbl y fld parameters supposed to mean? And how are they related to the code you posted? -

No olvide marcar su respuesta favorita ;-) -

1 Respuestas

In your first LINQ query, you are defining an anonymous type. If you want you can put almost the whole code block (both LQ y 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 (Person y PeopleID). However, this approach is a bit fragile, since there is no compile time support. Another option therefore is to define a special Class (llamémoslo PersonDTO) that contains two public properties Person y PeopleID and let that method return an IEnumerable(Of PersonDTO) or even better an array of PersonDTO objetos.

Respondido el 21 de Septiembre de 11 a las 11:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.