我正在尝试设置一个 LINQ 查询以用作组合框数据源。 组合框是 DisplayMember 和 PeopleID for ValueMember 的简单名称列表。
问题是名称可以是"第一个最后一个"或"最后一个,第一个"格式。 我有一个 GetFullName(intPeopleID) 函数来返回正确的名称序列。 无论哪种方式,组合框名称都必须按字母顺序排序,因此需要在第一个查询获取全名后进行排序。
Dim LQBackupOriginalA = From a In DCAppMain.tblPeopleMain
Where a.BusinessGroupID = intBusinessGroupID
Select Person = PP.GetFullName(a.PeopleID), a.PeopleID
Dim LQBackupOriginalB = From b In LQBackupOriginalA
Order By b.Person
Select b.Person, b.PeopleID
cboBackupOriginal.DataSource = LQBackupOriginalA
cboBackupOriginal.DisplayMember = "Person"
cboBackupOriginal.ValueMember = "PeopleID"
这是行不通的,因为函数"GetFullName"不会转换为SQL——当然。 我已经尝试了许多变体,但没有成功。
我可以先将第一个查询中的数据追加到本地表中,然后在设置组合框的数据源之前,使用对数据进行排序的查询将其拉回。 但我想知道是否有更直接的东西?
您应该能够只筛选所需的行,将查询转换为可枚举的行,并在内存中执行GetFullName
部分。我的 VB 缺少,但与此类似;
Dim LQBackupOriginalA = (
From a In DCAppMain.tblPeopleMain
Where a.BusinessGroupID = intBusinessGroupID
Select a.PeopleID).
AsEnumerable().
Select(Function(peopleId) New With {
Key .Person = PP.GetFullName(peopleId), .PeopleID = peopleId
})
。或者在 C# 中,我确定语法;
var LQBackupOriginalA =
DCAppMain.tblPeopleMain
.Where(a => a.BusinessGroupID == intBusinessGroupID)
.Select(a => a.PeopleID)
.AsEnumerable()
.Select(peopleId =>
new {Person = PP.GetFullName(peopleId), PeopleID = peopleId});
谢谢约阿希姆 - 到目前为止我还没有尝试使用 lambda 函数,它们很有帮助!
这是我最终所做的确实有效:
'-- Get Distinct ManagerID
Dim LQManagerOriginalA = (From a In DCAppMain.tblPeopleMain
Where a.ManagerID IsNot Nothing And a.CurrentEmployee = True And a.UserType = "Full"
Group By a.ManagerID Into g = Group
Order By ManagerID
Select New With {.ManagerID = ManagerID})
'-- Get Ordered List of FullNames and PeopleIDs
Dim LQManagerOriginalB = LQManagerOriginalA.[Select](Function(a) a.ManagerID).AsEnumerable().[Select](Function(a) New With {Key .Person = PP.GetFullName(a), Key .PeopleID = a}).OrderBy(Function(x) x.Person).ToList()
'-- Apply datasource to combobox
RemoveHandler cboManagerOriginal.SelectedIndexChanged, AddressOf cboManagerOriginal_SelectedIndexChanged
cboManagerOriginal.DataSource = LQManagerOriginalB
AddHandler cboManagerOriginal.SelectedIndexChanged, AddressOf cboManagerOriginal_SelectedIndexChanged
cboManagerOriginal.DisplayMember = "Person"
cboManagerOriginal.ValueMember = "PeopleID"