Equals() function with multiple values instead of one - vb.net

I have been using the following since a few years:
Dim mycol = xml.First(Function(x) CStr(x.Attribute("name")).Equals("Source"))
Now I would like to retrieve if either the name attribute is equal to "Source" or "Input".
Does any solution exist for this case?
I have tried the following without any success
Dim cases As String() = {"Source", "Input"}
Dim mycol = xml.First(Function(x) CStr(x.Attribute("name")).Equals(cases))

You would reverse the order of the comparison and use Contains:
Dim mycol = xml.First(Function(x) cases.Contains(CStr(x.Attribute("name"))))

Related

How can I obtain a directory's file list sorted by timestamp?

I am stuck on this sorting problem.
Private Sub ...
Dim oDirInfo As DirectoryInfo
Dim aoFSInfo() As FileSystemInfo
Dim asFiles() As String
FQPN is a fully qualified path name ending in "\*.*".
oDirInfo = New DirectoryInfo(FQPN)
Now into asFiles I want the files' names, sorted by the files' timestamps in ascending order. I presume, that oDirInfo.CreationTime plays a role here, but can not figure out how to use OrderBy properly.
aoFSInfo = oDirInfo.GetFileSystemInfos() '?
asFiles = aoFSInfo.OrderBy(...)
End Sub
Yes, that's LINQ and you can use this (method-)syntax:
asFiles = oFSInfo.
OrderBy(Function(fsi) fsi.CreationTime).
Select(Function(fsi) fsi.FullName).
ToArray()
If you don't like the ugly Function keyword you can use query syntax:
Dim orderedFiles = From fsi In oFSInfo
Order by fsi.iCreationTime Ascending
Select fsi.FullName
asFiles = orderedFiles.ToArray()
Even if these are two statements it's not slower than method syntax due to deferred execution.

Linq where 'not like' or ! <> equivalent in

I've got this working :
Dim locations = From row In allLocations.AsEnumerable()
Where row.Field(Of String)("PartNr") Is Nothing
Select row
It gets all the locations where there is no partnumber.
But i would like to do something like this to get everything except some record(s):
Dim locations = From row In allLocations.AsEnumerable()
Where row.Field(Of String)("PartNr") Not Like "12345"
Select row
A simple string contains (ie. equivalent to SQL's like '%substring%') can be done with String.Contains:
Dim locations = From row In allLocations.AsEnumerable()
Where not row.Field(Of String)("PartNr").Contains("12345")
Select row
I've found a solution for what i was looking for:
Dim rgx As New Regex("^G[A-Z]")
Dim alleLocaties = From row In beginQuery.AsEnumerable()
Where (Not rgx.IsMatch(row.Field(Of String)("Location"))
In this example i get all the locations that do not start with G followed by a letter.

Search DataTable with values from another table

I'm trying to search one DataTable with values from anotherDataTable using LINQ, but no progress so far... How to do it?
In example below i have table, in which i search, and PlTable, which has only one column; and i need to retrieve every row from table, in which the Name field contains at least one string of Name field in PlTable's rows.
Dim ePlTable As IEnumerable(Of DataRow) = PlTable.AsEnumerable()
Dim found = From row In table.AsEnumerable
Where row(0).Contains(ePlTable)
Select row
Return found.CopyToDataTable.Rows
Surely it does not work, as .Contains wants String as argument
Surely it does not work, as .Contains wants String as argument
That's exatly the problem, so use the strongly typed Field extension
method to cast it to it's correct type and Enumerable.Any to look if at least one string is contained in this Name:
Dim strings = From row In PlTable Select row.Field(Of String)(0)
Dim found = From row In table.AsEnumerable
Where strings.Any(Function(s) row.Field(Of String)("Name").Contains(s))
Select row
Return found.CopyToDataTable()

DataTable.Select.Where VB.Net - Delete rows

I'm currently pulling information using a query that I'm not allowed to tamper with:
Dim dt As DataTable = BLL.GetData(variable).Tables(0)
Immediately afterwards, I'm removing any records where a field begins with a specific value:
For Each dr As DataRow In dt.Rows
If dr.Item(2).ToString().StartsWith("value") Then
dr.Delete()
End If
Next
What I'd really like to do is something like:
dt.Select.Where(field1 => field1.StartsWith("value")).Delete()
I know that is not the syntax of it and I'm probably very off from what it would be like. The For Each works fine, I'm just trying to "simplify" it. Any idea? Any and all help is appreciated.
Actually, your initial code is probably the cleanest and most straight forward.
To delete items using LINQ, you first need to read them into a separate collection, then loop through that collection and call Delete on each record. If you'd rather go that route, you could try:
Dim records = dt.Rows.Where(Function(r) r.StartsWith("value")).ToList()
For Each r In records
r.Delete()
Next
The answer I think you are looking for is below from Microsoft. https://msdn.microsoft.com/en-us/library/det4aw50(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2
Dim table As DataTable = DataSet1.Tables("Orders")
' Presuming the DataTable has a column named Date.
Dim expression As String
expression = "Date > #1/1/00#"
Dim foundRows() As DataRow
' Use the Select method to find all rows matching the filter.
foundRows = table.Select(expression)
Dim i As Integer
' Print column 0 of each returned row.
For i = 0 to foundRows.GetUpperBound(0)
Console.WriteLine(foundRows(i)(0))
Next i

DataTable Select(String) Function Help VB .NET

I made a datatable with 2 columns a transactionTime column and a numberOfTransactions column. I made the table with the pre-defined transaction times and want to add the number of transactions from an XML file. I have gotten through the XML file and want to add the data to the correct row. Here is the function:
Function AddRow(ByVal timeOfTransaction As String, ByVal numberOfTransactions As String, ByRef dataTableOfTransactions As DataTable) As String
Dim row() As DataRow = dataTableOfTransactions.Select("transactionTime = timeOfTransaction")
If row(0) IsNot Nothing Then
row(0)("numberOfTransactions") = numberOfTransactions
End If
Return Nothing
End Function
When I run this it overwrites the first element in the table's numberOfTransactions coloumn. I know it has to do with the "transactionTime = timeOfTransaction" part but I can't seem to get it to read timeOfTransaction as a reference to a string instead of a literal. Any help would be much appreciated. Thanks!
You need to write something like this :
Dim row() As DataRow = dataTableOfTransactions.Select("transactionTime=#" & timeOfTransaction & "#")
But be careful with your date/month or month/date format, it depends of your regional settings.
row(0)("numberOfTransactions") = numberOfTransactions
Right there you are telling the program to overwrite that value with number of transactions.
If you want that value you need to set it to something, not set something to it.
Also, if you want your select to work properly try doing it like this
dataTableOfTransactions.Select("transactionTime = " + timeOfTransaction)