LINQ query for filtering

Can someone post code sample for the following suggestion made by @ppr in one of the posts please. Thanks a lot,

===========
(From s in searchtermlistvar
From r in yourdatatablevar.AsEnumerable
where r(YourColumnNameOrIndex).toString.Contains(s)
Select r).CopyToDataTable

keep in mind .CopyToDataTable thtows an exception if no rows are returned. Additional Handling of this is possible as well

=============

we can handle as following:

Assign activity:
LHS: Result | datatype: List(Of Datarow)
RHS:

(From s in searchtermlistvar
From r in yourdatatablevar.AsEnumerable
where r(YourColumnNameOrIndex).toString.Contains(s)
Select r).ToList

then based on the filter result we can copy it to a datatable or clone the origin one
check within an if acitvity
Result.Count > 0
Then: dtResult = Result.CopyToDataTable
Else: dtResult = yourdatatablevar.Clone

Keep in mind that maybe the LINQ requires a rework. Will check it and if needed will answer it on next post

Thank you, @ppr.

It should work. May we ask you to close the topic, once you have done with your testing. Thanks

@ppr The solution posted DT_Filter1Col_ByKeywordList.xaml in the original link worked for me. Thanks!

Why not just use Filter Datatable? This is what it’s for.

Time savings. Thank you,

LINQ doesn’t save time. Do you mean because you think it avoids looping? It doesn’t.

The Filter Datatable activity probably just uses LINQ internally anyway.

Perfect:

this is the array with Keywords:
grafik

this is the origin data:
grafik

this is output:
grafik

So just share your demo xaml with us where you used the filterdatatable activity implementing this case (column value is item from the KeyWords array)

Thanks for support

image

No time difference for over 1000 rows.

image

Still no time difference for almost 12000 rows.

image

119,000 rows and I just realized the LINQ query I wrote is only looking for one item, not two items.

image

NOW will people stop worrying about thinking avoiding For Each etc saves time?

EDIT: I updated the LINQ query to ppr’s with the outside loop through Filterlist…

image

I was not talking about time differences.

So you was forced to loop over the Keywords
Also you was not running over the given Set.

With modified keywords: A, Apple
And dataset:

|A  |1|
| Angora| 2|

Your contains approach would fail. When Datacolumn values are to trim (see above) a change to equal (=) also would fail.

It is not about that it is not possible to get involved a filter datatable into an implementation. But if it results into more items to model, then it is also valid to solve it more compact.

But we do also have other implementation options. Another one is using the select method and using the Operator IN

So why not having the choice and selecting the approach which is individually preferred. I dont see a reason why Filter datatable is a must to use it. Also when it has some weakness e.g. the case from above where a trim is needed

1 Like

So does your LINQ query.

“Your contains approach would fail”

No it wouldn’t. Contains isn’t broken by spaces.

" I dont see a reason why Filter datatable is a must to use it. "

It’s not a must. I’m simply dispelling (again) the myth that it should be avoided to save time. The benefit is that it’s easier to understand for those who may come after you and have to maintain your code. It makes it easier to visualize what is happening.

This is a good read.

run it show us the dump.
Contains will catch also Angora as it is matching on A, But Angora is not within the Keyword list

Again: I am not talking about timings

However, this side discussion can be closed as it is not adding new insights and as long not a running proof is shared the argumentation for the filter data activity looks questionable related to the case

keywords: A, Apple

|A  |1|
| Angora| 2|

not catching A (when =) and matching Angora (when contains)

Well yeah, that’s how contains works. If you’re looking for A you do contains(“A”) and if you’re looking for “Angora” you do contains(“Angora”) - it’s not the spaces causing the issue, it’s not using the correct search term.

" Again: I am not talking about timings"

The OP is.

Your “A, Apple” point is silly. All you’re doing is changing what you want to look for.