When I say simply say “select c”, the following code works, but I want to select the id, first name and last name, not the entire row as it is fairly lengthy.
I am fairly new to Linq. I had this working with anonymous types, but have been instructed to define all variables (hence all the as “IEnumeralbe(of DataRow)”‘s)..but none of the examples I have come across use “Select” the way I want to and they don’t tend to go on with the for loop using the fields selected in the query.
Any advice would be greatly appreciated.
Thanks in advance!!
Dim cands As IEnumerable(Of DataRow) = ds.Tables("Candidates").AsEnumerable()
Dim candPlace As IEnumerable(Of DataRow) = ds.Tables("JobCandPlacement").AsEnumerable()
Dim candComp As IEnumerable(Of DataRow) = ds.Tables(dctSearchCriteria.Values(i).ToString).AsEnumerable()
Dim strCandID As String = ""
Dim candMatches As IEnumerable(Of DataRow) = From c In cands, cc In candComp, cp In candPlace
Where c.Field(Of String)("candidateID") = cc.Field(Of String)("candID") _
AndAlso cc.Field(Of String)("compSkill") = cSkill _
Select c("candidateID"), c("firstName"), c("lastName")
For Each cm As DataRow In candMatches
strCandID = ""
strCandID = cm("candidateID")
increaseCandScore(strCandID)
If Not dtSuitableCands.Rows.Contains(strCandID) Then
Dim dr As DataRow = dtSuitableCands.NewRow
dr("candID") = cm("candidateID")
dr("candFName") = cm("firstName")
dr("candLName") = cm("lastName")
AddNewRow(dr)
End If
Next
There is no point. The entire table is already in memory, so all you’ll be doing is adding overhead.
LINQ never copies values, so the only way to optimize this is to get rid of the columns completely.
To answer the question, you need to select an anonymous type:
You can also use your current syntax, which is equivalent to this.
Since you’re creating an anonymous type, you cannot declare it as
IEnumerable(Of DataRow); you must useOption Inferand writeDim candMatches = From ...