如何将 Linq to SQL 类型传递给函数

发布于 2024-12-01 20:36:28 字数 837 浏览 1 评论 0原文

下面的代码在 Linq to SQL 解决方案中使用,位于表单的 Load 事件中。

它可以很好地显示人员姓名的有序列表,并将值成员设置为该人员的 ID。

但是,我将经常使用这些“人员”组合框,因此我想将一个子项放入我的实用程序代码中,表单的加载事件中只有一行。

我想调用类似的内容: Call ComboboBoxPeople(cbo, tblTurnbackMain, ReportedByID)

子程序的开头如下: Public Sub ComboboxPeople(cbo as Combobox, tbl as 'sometype', fld as 'someothertype ')

我可以使用什么作为 tblfld 的参数类型?

'-- 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"

谢谢! 担

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

合约呢 2024-12-08 20:36:28

在您的第一个 LINQ 查询中,您定义了一个匿名类型。如果您愿意,您可以将几乎整个代码块(LQLT)放入一个方法中并返回 LT。由于 LT 包含 IEnumerable(Of T),其中 T 是匿名类型,因此您可以让您的方法仅返回 IEnumerable< /code>,如下所示:

Public Shared Function GetAllPeople() As IEnumerable
   ' code here
End Function

这将在 ASP.NET Web 窗体中工作,因为 Web 窗体使用反射从定义的成员(PersonPeopleID)加载数据。然而,这种方法有点脆弱,因为没有编译时支持。因此,另一个选择是定义一个特殊的 Class(我们称之为 PersonDTO),其中包含两个公共属性 PersonPeopleID > 并让该方法返回一个 IEnumerable(Of PersonDTO) ,甚至更好的是一个 PersonDTO 对象数组。

In your first LINQ query, you are defining an anonymous type. If you want you can put almost the whole code block (both LQ and LT) in a single method and return LT. Since LT contains an IEnumerable(Of T) with T being an anonymous type, you can let your method simply return an IEnumerable, as follows:

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 and PeopleID). However, this approach is a bit fragile, since there is no compile time support. Another option therefore is to define a special Class (let's call it PersonDTO) that contains two public properties Person and PeopleID and let that method return an IEnumerable(Of PersonDTO) or even better an array of PersonDTO objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文