试试这个:
Dim strS As String = ""
Dim dt As DataTable = objDataSet.Tables(1)
' create a lambda expression to split the name by " "
Dim splitName = Function(name As String) name.Split(" "c).Where(Function(s) s <> "")
' select rows that meet the SID filter and SName <> null and SName <> "",
' and get the SID, construct the FirstName and LastName,
' and of course, sort the return collection by LastName
Dim sortedrows = From row As DataRow In dt.Rows
Where (Not IsDBNull(row!SName) AndAlso Not String.IsNullOrWhiteSpace(CStr(row!SName)))
And CStr(row!SID) = strSID
Select PersonID = row!PersonID,
FirstName = splitName(CStr(row!SName)).FirstOrDefault,
LastName = splitName(CStr(row!SName)).LastOrDefault
Order By LastName
' finally, construct the <a> tags as required from above
For Each row In sortedrows
strS &= String.Format("<a href=""javascript: ViewPersonId({0}); return false;"">{1} {2}</a><br />", row.PersonID, row.FirstName, row.LastName)
Next
如果 DataTable 包含以下行:
dt.Rows.Add(1, "John Travolta")
dt.Rows.Add(2, "Jack The Ripper")
dt.Rows.Add(3, "Beyonce")
dt.Rows.Add(4, " ")
dt.Rows.Add(5, "")
dt.Rows.Add(6, DBNull.Value)
假设他们都满足 SID 过滤器,它将返回:
<a href="javascript: ViewPersonId(3); return false;">Beyonce Beyonce</a><br />
<a href="javascript: ViewPersonId(2); return false;">Jack Ripper</a><br />
<a href="javascript: ViewPersonId(1); return false;">John Travolta</a><br />
如您所见,"Beyonce"
显示为"Beyonce Beyonce"
。如果您不希望这种行为,可以通过在For Each
循环中检查 FirstName = LastName 并执行替代代码来构造<a>
标签来更改它。