Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
When attempting to work with a dataview, its easy to filter on something = something. But what I want to do is filter on something contains this_string.

I have a data table with about 5000 items in it. Where in PowerShell I can filter using something like the following:

$filteredOnContainsString = $datatable.rows | ? {$_.Contains(String)}

This works, but is is super slow.

Would someone please give me an example where I can filter with contains or something similar to get the same results as above?

What I have tried:

Tied this:

$dataviewFilter = New-Object System.Data.dataview($datatable)
$dataviewFilter.RowFilter = "Name contains Del"

Result = "Exception setting "RowFilter": "Syntax error: missing operand after the 'Contains' operator."
Posted
Updated 10-Jun-19 6:10am

"Probably" the whole table is getting downloaded due to the ".Contains" extension.

You need a stored proc or pass-thru SQL so the query "runs on the server"

Quote:
Now, I mentioned earlier about the Invoke-SQLCMD cmdlet. This is by far the easiest way to get data in and out of SQL Server in PowerShell. Set a string variable to the query you want, and use the cmdlet. If you’re getting results back, assign the invocation of the cmdlet to a variable, and those results are returned as a PowerShell object.


In PowerShell ADO.NET is Still Your Friend – Allen White SQL Server Performance and Automation Blog[^]
 
Share this answer
 
Comments
[no name] 18-Jun-19 10:45am    
thanks!
I actually do not have permissions to an SQL server, so I am unsure if that is even possible for me to run "Invoke-SQLCMD". What I am doing is querying a SharePoint site using invoke-restmethod in PowerShell, as I only have Full control permissions on a site that I manage. I was successful in filling a Datatable.
Now my goal is to filter the data.
Taking your response is purly SQL based, that got me to think that the datable object can only be filtered based on SQL syntax.

So where this works for powershell: $filteredOnContainsString = $datatable.rows | ? {$_.Contains(String)}

The equivalent SQL query would be something like this:
Select [Table] from [Database]
where [column] like '%string%'


Taking this to my Datatable filter this actually works!!

$dataviewFilter = New-Object System.Data.dataview($datatable)
$dataviewFilter.RowFilter = "Name like '%Del%'"


SO the moral of the story is SYNTAX. IN this case the Syntax is in SQL format. My bad. Thank you for sending me in the correct direction!!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900